0

I would like to iterate through a comma separated list to translate some words. I already found how to do so using \docsvlist from the etoolbox package. However, there is a large space printed before the first word and between the words when there should only be a "normal" space after the printed comma. I think the problem comes from the nesting of the \ifstrequal commands used for the translation.

Below is a MWE including an example calling all the possible translations and another example calling only the first translation to show the impact of the nesting.

How can I avoid these additional spaces?

MWE

\documentclass{article}

\usepackage{etoolbox}

\begin{document}

\newcommand\TranslateAnimal[1]{
    \ifstrequal{#1}{Cat}{Chat}{
        \ifstrequal{#1}{Dog}{Chien}{
            \ifstrequal{#1}{Mouse}{Souris}{
                \ifstrequal{#1}{Bird}{Oiseau}{
                    \ifstrequal{#1}{Horse}{Cheval}{
                        #1 % If there is no known translation
    }}}}}
}

\newcommand{\TranslateAnimalList}[2][,]{
    \def\nextitem{\def\nextitem{#1}}% Separator
    \renewcommand*{\do}[1]{\nextitem\TranslateAnimal{##1}} % How to process each item
    \docsvlist{#2}% Process list
}

There are additional large spaces before the first word and after words: \TranslateAnimalList{Cat,Bird,Dog,Horse,Mouse,Seal,Donkey}

Compare with: \TranslateAnimalList{Cat,Cat,Cat,Cat,Cat,Cat,Cat} where there is only a large space before the first word

\end{document}

AlMa
  • 560

1 Answers1

2

Check the differences and look at What is the use of percent signs (%) at the end of lines? (Why is my macro creating extra space?)

\documentclass{article}

\usepackage{etoolbox}

\newcommand\TranslateAnimal[1]{% \ifstrequal{#1}{Cat}{Chat}{% \ifstrequal{#1}{Dog}{Chien}{% \ifstrequal{#1}{Mouse}{Souris}{% \ifstrequal{#1}{Bird}{Oiseau}{% \ifstrequal{#1}{Horse}{Cheval}{% #1% If there is no known translation }}}}}% }

\newcommand{\TranslateAnimalList}[2][, ]{% \def\nextitem{\def\nextitem{#1}}% Separator \renewcommand*{\do}[1]{\nextitem\TranslateAnimal{##1}}% How to process each item \docsvlist{#2}% Process list }

\begin{document}

The list is \TranslateAnimalList{Cat,Bird,Dog,Horse,Mouse,Seal,Donkey}.

Compare with \TranslateAnimalList{Cat,Cat,Cat,Cat,Cat,Cat,Cat}.

\end{document}

You can possibly appreciate a different implementation with expl3.

\documentclass{article}
%\usepackage{xparse} % not needed with LaTeX 2020-10-01 or later

\ExplSyntaxOn \NewExpandableDocumentCommand{\TranslateAnimal}{m} { \str_case:nnF { #1 } { {Cat}{Chat} {Dog}{Chien} {Mouse}{Souris} {Bird}{Oiseau} {Horse}{Cheval} } { #1 } % no known translation }

\NewDocumentCommand{\TranslateAnimalList}{O{,~}m} { \seq_clear:N \l_tmpa_seq \clist_map_inline:nn { #2 } { \seq_put_right:Nn \l_tmpa_seq { \TranslateAnimal { ##1 } } } \seq_use:Nn \l_tmpa_seq { #1 } } \ExplSyntaxOff

\begin{document}

The list is \TranslateAnimalList{Cat,Bird,Dog,Horse,Mouse,Seal,Donkey}.

Compare with \TranslateAnimalList{Cat,Cat,Cat,Cat,Cat,Cat,Cat}.

\end{document}

enter image description here

egreg
  • 1,121,712