语境
我正在尝试在环境中将多幅图像粘贴到一行中。我尝试使用包中的tabularx
可选命令来实现这一点。它由索引、餐点和相应的价格组成。我想添加过敏原的图像,但将其作为可选参数。此外,我事先不想预设餐点中包含哪种过敏原或过敏原的数量。为此,我尝试从同一个包中实现命令。我试过在 Google 上搜索这个问题,并尝试了以下代码。我定义的命令检查是否使用提供了可选参数,但它似乎导致了我无法摆脱的行距问题:\NewDocumentCommand
xparse
\ProcessList
xparse
\Entry
\IfNoValueTF
代码(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}
输出
问题
我的主要问题是:在使用可选参数与命令结合时,如何摆脱(对我来说)意外的行距问题\mylist
?我想使用第三个表格组的语法,但我期望并希望输出与第二个表格组相同。
我还有一个问题,为什么第 3 行和第 4 行的餐点和图像之间会有额外的空间,以及如何消除这种情况。
对于非库存图片/pdf 深感抱歉,我不知道对于这个临时问题使用库存图片的最简单方法。
答案1
您的定义中\Entry
有一个空白行(即段落分隔符)。此外,我通过添加%
行尾来删除该定义中的杂散空格。
根据 OP 的后续评论,我还\mylist
通过添加两个额外的行尾%
标记修复了宏中的杂散空格问题。
\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}