🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.text.tex
Article #490

Re: How to make a macro expand to nothing?

#490
From: Donald Arseneau
Date: Tue, 28 Jul 1992 04:53
72 lines
3206 bytes
In article <1992Jul27.213015.29910@bmerh85.bnr.ca>, crath@bnr.ca writes...
In article <1992Jul27.213015.29910@bmerh85.bnr.ca>, you write...
>I am using the following latex macro:
> 
>	\newcommand{\Ch}[2]{{#2}}
> 
>When used, it produces the following output from the given input:
> 
>	input:   This is \Ch{G}{a} sam\Ch{C}{ple.} \Ch{G}{}
> 
>	output:  This is a sample.
> 
>This works as expected except ... 
>extra whitespace gets inserted after the paragraph.  

The problem isn't with the macro expanding to a space, or with the 
end of the paragraph.  The extra space is the space between your two 
final \Ch commands.  

        input: This is \Ch{G}{} a sample.
        output: This is  a sample.

If you want this to look nice for whole words and parts of words, you
are probably best including the space in the parameter:

\newcount\plurality \plurality=2  % LaTeX's versions of counters don't work
                                  % well for this.

\newcommand{\pl}[2]{{\ifnum1=\plurality #1\else #2\fi}}

\pl{This}{These} \pl{is}{are}\pl{ a}{} sample\pl{}{s}.
%                          NOTE " a"


Here is a fair attempt at handling the spaces automatically:
(put these in a style file so @ is valid in command names.)

\newcounter{plurality} 
% If LaTeX-style counters are really necessary.

\newcommand{\pl}[2]{\leavevmode{\ifnum\@ne=\c@plurality 
  \plU{#1}\else\plU{#2}\fi}}
% ignore spaces afterwards if blank entry follows a space.
\newcommand{\plU}[1]{\skip@\lastskip #1%
  \ifdim\skip@=\z@\else \if\blank{#1}\aftergroup\ignorespaces\fi\fi}

%==============Conditionals by Donald Arseneau==============================
%  \ifblank --- checks if parameter is blank (Spaces count as blank)
%  \ifgiven --- checks if parameter is not blank: like \ifblank\else
%  \ifnull  --- checks if parameter is null (spaces are NOT null)
{\catcode`\!=8 % funny catcode so ! will be a delimiter
%.. use \if\given, \if\blank, \if\naught (\null already exists)
%
\long\gdef\given#1{\fi\Ifbl@nk#1@@@\empty!}
\long\gdef\blank#1{\fi\Ifbl@nk#1@@..!}% if null or spaces
\long\gdef\naught#1{\IfN@Ught#1* {#1}!}% if null
\long\gdef\Ifbl@nk#1#2@#3#4!{\ifx#3#4}
\long\gdef\IfN@Ught#1 #2!{\blank{#2}}
}
%==================================

TEST: 

\setcounter{plurality}{1}%  
\pl{This}{These} \pl{is}{are} \pl{a}{} sample\pl{}{s}.

\setcounter{plurality}{2}%  
\pl{This}{These} \pl{is}{are} \pl{a}{} sample\pl{}{s}.
\setcounter{plurality}{3}%  
\pl{This}{These} \pl{is}{are} \pl{a}{} sample\pl{}{s}.

Donald Arseneau        asnd@reg.triumf.ca
Technical Details
Message-ID: <27JUL199221533271@reg.triumf.ca>
Path: arc1-ca.pugleaf.net!sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!yale!gumby!destroyer!ubc-cs!unixg.ubc.ca!reg.triumf.ca!asnd
References: <1992Jul27.213015.29910@bmerh85.bnr.ca>

Show Full Headers
Complete Article Headers
Newsgroups: comp.text.tex
Path: arc1-ca.pugleaf.net!sparky!uunet!cs.utexas.edu!qt.cs.utexas.edu!yale.edu!yale!gumby!destroyer!ubc-cs!unixg.ubc.ca!reg.triumf.ca!asnd
From: asnd@reg.triumf.ca (Donald Arseneau)
Subject: Re: How to make a macro expand to nothing?
Message-ID: <27JUL199221533271@reg.triumf.ca>
News-Software: VAX/VMS VNEWS 1.41    
Keywords: macro latex tex
Sender: news@unixg.ubc.ca (Usenet News Maintenance)
Nntp-Posting-Host: reg.triumf.ca
Organization: TRIUMF: Tri-University Meson Facility
References: <1992Jul27.213015.29910@bmerh85.bnr.ca>
Date: Tue, 28 Jul 1992 04:53:00 GMT
Lines: 72