4

Context

I am trying to paste multiple images in one row within a tabularx environment. Using optional commands with the \NewDocumentCommand from the xparse package I tried to implement this. It constist of having an index, a meal and a corresponding price. I want to add images of allergens, but have this as an optional argument. Also, beforehand I do not want to preset which or the number of allergens the meal contains. To do so I attempted to implement the \ProcessList command from the same xparse package. I have tried to Google the problem and tried the following code. The \Entry command I defined checks if the optional argument is provided by using \IfNoValueTF, but it seems to cause row-spacing issues that I cannot get rid of:

Code (MWE)

\documentclass[11pt]{article}
\usepackage{xparse}
\usepackage{graphicx}
\usepackage[textwidth=24cm,centering,margin=2cm]{geometry}
\usepackage{tabularx}

% Columntypes
\newcolumntype{N}{>{\raggedright\let\newline\\\arraybackslash}p{1.5em}}
\newcolumntype{P}{>{\raggedleft\let\newline\\\arraybackslash}p{2.5em}}

% Tabular environment
\newenvironment{Group}
        {\noindent\tabularx{\linewidth}{@{}NXP@{}}\\[0.5em]}
        {\endtabularx
}

% Entries with optional argument
\newcounter{Entry}[section]

\NewDocumentCommand\Entry{smmO{}}{%
    \IfBooleanTF{#1}{%
        \IfNoValueTF{#4}{
            #2 \dotfill € & #3 \ignorespaces
        }{
            #2 \mylist{#4} \dotfill € & #3 \ignorespaces
        }
    }{%
        \IfNoValueTF{#4}{
            \refstepcounter{Entry}\theEntry. & #2 \dotfill € & #3
        }{
              \refstepcounter{Entry}\theEntry. & #2 \mylist{#4} \dotfill € & #3
        }

    }
}


% Paste optional items next to each other
\NewDocumentCommand\mylist{>{\SplitList{,}}m}
  {
      \ProcessList{#1}{\insertitem}
  }
  
\newcommand\insertitem[1]{\includegraphics[height = 1.5em]{#1.pdf}}

\begin{document}

Normal row spacing using `hardcoded' syntax, unexpected hspace when using mylist command \\

\noindent\begin{tabularx}{\linewidth}{@{}NXP@{}}
1. & meal \includegraphics[height=1.5em]{pepper.pdf}\includegraphics[height=1.5em]{pepper.pdf} \dotfill € & 4.00 \\
2. & meal \includegraphics[height=1.5em]{pepper.pdf}\includegraphics[height=1.5em]{pepper.pdf} \dotfill € & 4.00 \\
3. & meal \mylist{pepper, pepper} \dotfill € & 4.00 \\
4. & meal \mylist{pepper, pepper} \dotfill € & 4.00 \\
\end{tabularx}

\vspace{2cm}

Normal row-spacing when using normal includegraphics commands \\
\begin{Group}
1. & meal \includegraphics[height=1.5em]{pepper.pdf}\includegraphics[height=1.5em]{pepper.pdf} \dotfill € & 4.00 \\
2. & meal \includegraphics[height=1.5em]{pepper.pdf}\includegraphics[height=1.5em]{pepper.pdf} \dotfill € & 4.00 \\
\end{Group}

\vspace{2cm}

Unexpected row-spacing when using optional argument for images \\
\begin{Group}
\Entry{meal}{4.00}[{pepper,pepper}] \\
\Entry{meal}{4.00}[{pepper,pepper}] \\
\end{Group}

\end{document}

Output

enter image description here

Question

My main question is: How do I get rid of the (for me) unexpected row-spacing issues while using optional arguments combined with \mylist command? I want to use the syntax for the third tabular group, but I expected and want the output to be identical to the second tabular group.

A side question I have is how/why there is additional space between the meal and the image in rows 3 and 4, and how this can be undone.

Deeply sorry for the not-stock picture/pdf, I am not aware of the easiest way to use stock images for this ad-hoc question.

1 Answer 1

5

Your definition of \Entry had a blank line (i.e., paragraph break) in it. Also, I removed stray spaces in that definition by adding % to line ends.

Per the OP's follow-up comment, I also fixed the stray-space issue in the \mylist macro, by adding two additional line-ending % tokens.

\documentclass[11pt]{article}
\usepackage{xparse}
\usepackage{graphicx}
\usepackage[textwidth=24cm,centering,margin=2cm]{geometry}
\usepackage{tabularx}

% Columntypes
\newcolumntype{N}{>{\raggedright\let\newline\\\arraybackslash}p{1.5em}}
\newcolumntype{P}{>{\raggedleft\let\newline\\\arraybackslash}p{2.5em}}

% Tabular environment
\newenvironment{Group}
        {\noindent\tabularx{\linewidth}{@{}NXP@{}}\\[0.5em]}
        {\endtabularx
}

% Entries with optional argument
\newcounter{Entry}[section]

\NewDocumentCommand\Entry{smmO{}}{%
    \IfBooleanTF{#1}{%
        \IfNoValueTF{#4}{%
            #2 \dotfill € & #3 \ignorespaces
        }{%
            #2 \mylist{#4} \dotfill € & #3 \ignorespaces
        }%
    }{%
        \IfNoValueTF{#4}{%
            \refstepcounter{Entry}\theEntry. & #2 \dotfill € & #3%
        }{%
              \refstepcounter{Entry}\theEntry. & #2 \mylist{#4} \dotfill € & #3%
        }%
    }%
}


% Paste optional items next to each other
\NewDocumentCommand\mylist{>{\SplitList{,}}m}
  {%
      \ProcessList{#1}{\insertitem}%
  }
  
\newcommand\insertitem[1]{\includegraphics[height = 1.5em]{#1.pdf}}

\begin{document}

Normal row spacing using `hardcoded' syntax, unexpected hspace when using mylist command \\

\noindent\begin{tabularx}{\linewidth}{@{}NXP@{}}
1. & meal \includegraphics[height=1.5em]{example-image.pdf}\includegraphics[height=1.5em]{example-image.pdf} \dotfill € & 4.00 \\
2. & meal \includegraphics[height=1.5em]{example-image.pdf}\includegraphics[height=1.5em]{example-image.pdf} \dotfill € & 4.00 \\
3. & meal \mylist{example-image, example-image} \dotfill € & 4.00 \\
4. & meal \mylist{example-image, example-image} \dotfill € & 4.00 \\
\end{tabularx}

\vspace{2cm}

Normal row-spacing when using normal includegraphics commands \\
\begin{Group}
1. & meal \includegraphics[height=1.5em]{example-image.pdf}\includegraphics[height=1.5em]{example-image.pdf} \dotfill € & 4.00 \\
2. & meal \includegraphics[height=1.5em]{example-image.pdf}\includegraphics[height=1.5em]{example-image.pdf} \dotfill € & 4.00 \\
\end{Group}

\vspace{2cm}

Unexpected row-spacing when using optional argument for images \\
\begin{Group}
\Entry{meal}{4.00}[{example-image,example-image}] \\
\Entry{meal}{4.00}[{example-image,example-image}] \\
\end{Group}

\end{document}

enter image description here

4
  • Wow, how infuriating but also logical! Also thank you for pointing out that example-image.pdf works for this use case. Do you happen to know about the extra space in rows 3 and 4 between the images and the meals?
    – PLY
    Apr 19, 2022 at 19:48
  • 1
    @PLY Again it is a case of stray spaces in the definition of \mylist. Note my two added % in this definition: \NewDocumentCommand\mylist{>{\SplitList{,}}m} {% \ProcessList{#1}{\insertitem}% }. I have edited my answer to include this fix, as well. Apr 19, 2022 at 20:15
  • 1
    @PLY FYI: if a line of source code ends on a control sequence name (e.g., macro), one need not terminate the line with a % to avoid a stray space. Apr 19, 2022 at 20:21
  • makes perfectly sense! I was already wondering seeing other people's code why the % is there as I thought it was simply for better structure. Will add the % to my code from now on too!
    – PLY
    Apr 19, 2022 at 22:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .