3

I am building a package for typesetting Celtic music tunebooks. One feature I make available is the command \altnames, which takes a comma-delimited list of alternate tune names as an argument. Below is an MWE of the \altnames command presented in a single document file:

\documentclass{article}
\makeatletter

\newcounter{loopitemcounter}\setcounter{loopitemcounter}{0}

\def\listingloopENDMARKER{\listingloopENDMARKER}

\long\def\listingloop#1in#2#3{%
   \looppicker{#1}{#3}#2,\listingloopENDMARKER,%
}

\long\def\looppicker#1#2#3,{%
   \def\tempitem{#3}%
   %\ifx\tempitem\listingloopENDMARKER
   \ifx\listingloopENDMARKER\tempitem %
      \let\next=\relax%
   \else %
      \def#1{#3}%
      \ifnum\value{loopitemcounter}=0
         #2%
      \else %
         ,\space#2%
      \fi
      \def\next{\looppicker{#1}{#2}}%
   \fi
   \stepcounter{loopitemcounter}
   \next
}

\newcommand{\altnames}[1]{%
   \listingloop\@picked@name in{#1}{%
      \textit\@picked@name
   }
}

\makeatother
\begin{document}
\altnames{The Day After,Trump? Really?,The End Days}
\end{document}

The \looppicker command prints each tune name parsed from the list, following each by a comma and a space (except for the last one). The result I am getting is the following: Result of MWE above, showing spaces after tune neames

The comma and space added between names by \looppicker is as intended, but as you can see an extra space is added immediately after each name, before the comma. How can I avoid this?

The \listingloop\@picked@name in{#1}{% line in \altnames, I thought, was the source, specifically the space before the word "in." This made sense (to me), as the "in" is the delimiter, and everything before it--including the space--becomes #1 in the \listingloop command. As you would expect, simply removing the space wreaks havoc. I tried using _in_ as the delimiter instead, but I get the same result. So, now I am thinking the \listingloop\@picked@name in{#1}{% line is not the culprit. Any insight is greatly appreciated. This one has me stumped, and I suspect there is something deeper and more subtle going on with how TeX macro definitions and their arguments are parsed, and I simply don't understand it.

3

Add a % after \stepcounter{loopitemcounter} in the definition of \looppicker:

\long\def\looppicker#1#2#3,{%
   \def\tempitem{#3}%
   %\ifx\tempitem\listingloopENDMARKER
   \ifx\listingloopENDMARKER\tempitem %
      \let\next=\relax%
   \else %
      \def#1{#3}%
      \ifnum\value{loopitemcounter}=0
         #2%
      \else %
         ,\space#2%
      \fi
      \def\next{\looppicker{#1}{#2}}%
   \fi
   \stepcounter{loopitemcounter}% <- here
   \next
}
3
  • Well, didn't see that coming. Thank you sooo much. Might I ask why putting that % there makes a difference? I thought I had a handle on the % sign thing, but apparently not. Dec 29 '16 at 4:54
  • @celticflute -- Why did you think it didn't add a space? You seem to be using % even in places where the % sign won't make a difference. (Not meant as a criticism: there are editor-specific reasons why I add % to line ends even though they make no difference to *TeX.)
    – jon
    Dec 29 '16 at 5:56
  • @jon -- (Sigh. Nothing for it ...) I guess I really have no clue about when a % sign is needed and when it isn't. Is there any definitive discussion of this subject to which you can point me? (Maybe it is time to get the TexBook.) Jan 5 '17 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.