我有一些来自实验的数字;它们用整数索引,并且它们并不总是连续的。给定索引列表,我需要将它们放在tabular
4 列中。经过一番搜索,我得到了这个:
\begin{table}[H]
\begin{tabular}{|c|c|c|c|}
\hline
\newcounter{imagecounter}
\foreach \n in {2,3,10,11,13,14,16,17,18,19,24,25,26,31,32,33,34,35,39,40}{
\stepcounter{imagecounter}
\ifnum\intcalcMod{\value{imagecounter}}{4}=0
& \includegraphics{image_index\n.png} \\
\fi
\ifnum\intcalcMod{\value{imagecounter}}{4}=1
\includegraphics{image_index\n.png}
\fi
\ifnum\intcalcMod{\value{imagecounter}}{4}=2
& \includegraphics{image_index\n.png}
\fi
\ifnum\intcalcMod{\value{imagecounter}}{4}=3
& \includegraphics{image_index\n.png}
\fi
}
\end{tabular}
\end{table}
问题是,显然,IF 控制器必须位于tabular
单元内部(是这样的吗?)。我还没能解决这个问题,也没能找到替代方案。
有没有简单的方法可以解决这个问题?还有其他方法吗?
如果是这样,请提供一个示例,我对宏、新命令以及任何非直接格式化文本和表格的东西都很陌生。
答案1
\foreach
如果确实想在表格中使用,这里有或多或少标准的方法。
(编辑扩展代码以涵盖图像数量不是列数倍数的情况)
我照做了,并使用了一个 \intcalcMod
宏,但更经济的做法是不使用类似的东西,而是在每一行之后将图像计数器重置为零。接下来添加了此替代方案。
\documentclass[border=12pt]{standalone}
\usepackage{pgffor}
\newcommand*{\intcalcMod}[2]{\numexpr #1+#2-((#1+(#2/2))/#2)*#2\relax}
\newcounter{imagecounter}
\usepackage{graphicx}
\begin{document}
\begin{tabular}{|l|l|l|l|}
\hline
\def\TEMP {}%
\foreach \n in
{2,3,10,11,13,14,16,17,18,19,24,25,26,31,32,33,34,35,39,40}%
{\stepcounter{imagecounter}%
\xdef\TEMP {\unexpanded\expandafter{\TEMP}\noexpand
\includegraphics[width=2cm]{example-image.png}%
(\n)}% (put it in the filename for the final version)
\ifnum\intcalcMod{\value{imagecounter}}{4}=0
\expandafter\gdef\expandafter\TEMP\expandafter{\TEMP \\}%
\else
\expandafter\gdef\expandafter\TEMP\expandafter{\TEMP &}%
\fi
}%
\TEMP
% added for the case the total number of images is not multiple of 4
\ifnum\intcalcMod{\value{imagecounter}}{4}=0
\else\expandafter\\
\fi
%
\hline
\end{tabular}
\end{document}
最好不要使用任何\intcalcMod
东西:
\hline
\def\TEMP {}%
\foreach \n in
{2,3,10,11,13,14,16,17,18,19,24,25,26,31,32,33,34,35,39,40}%
{\stepcounter{imagecounter}%
\xdef\TEMP {\unexpanded\expandafter{\TEMP}\noexpand
\includegraphics[width=2cm]{example-image.png}%
(\n)}% (put it in the filename for the final version)
\ifnum\value{imagecounter}=4
\expandafter\gdef\expandafter\TEMP\expandafter{\TEMP \\}%
\setcounter{imagecounter}{0}%
\else
\expandafter\gdef\expandafter\TEMP\expandafter{\TEMP &}%
\fi
}%
\TEMP
\ifnum\value{imagecounter}=0
\else\expandafter\\
\fi
%
\hline
答案2
另一个假设你无论如何都可以使用 TikZ,并且你再次有 4 的倍数(因为它给出了索引错误使用 PGF 数组查找(可以通过在开始时测试实际图像数量来修复)。
\documentclass[tikz]{standalone}
\usetikzlibrary{matrix}
\def\imarray{{2,3,10,11,13,14,16,17,18,19,24,25,26,31,32,33,34,35,39,40}}
\def\TEMP{}
\foreach\x in {1,...,5}{% Row num, just guessing.
\foreach\y[evaluate={\mynum= {\imarray[int((\x-1)*4+\y-1)]};}
] in {1,...,4}{%Col num 4 given
\xdef\TEMP{\expandafter\unexpanded\expandafter{\TEMP}%
\noexpand\node{\noexpand\includegraphics[width=2cm]{example-image} (\mynum)};%
\ifnum\y=4 \noexpand\\\else\noexpand\&\fi%
}%
}%
}
\begin{document}
\begin{tikzpicture}
\matrix[ampersand replacement=\&] (m) {\TEMP};
\end{tikzpicture}
\end{document}
我们尝试收集以下主题将活动字符和扩展敏感字符附加到宏
答案3
我会使用expl3
,它可以实现以下几个功能:
\documentclass{article}
\usepackage{graphicx}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\loopimages}{O{}m}
{% #1 is a list of options, #2 is the list of numbers
\leon_loopimages:nn { #1 } { #2 }
}
\keys_define:nn { leon/loopimages }
{
name .tl_set:N = \l_leon_loop_name_tl,
columns .int_set:N = \l_leon_loop_columns_int,
columns .initial:n = 4,
padding .dim_set:N = \l_leon_loop_padding_dim,
padding .initial:n = 1em,
last .choice:,
last/center .code:n = \tl_set:Nn \l_leon_loop_align_tl { \centering },
last/flush .code:n = \tl_set:Nn \l_leon_loop_align_tl { \raggedright },
last .initial:n = flush,
leading .dim_set:N = \l_leon_loop_leading_dim,
leading .initial:n = 1ex,
}
\dim_new:N \l_leon_loop_width_dim
\cs_new_protected:Npn \leon_loopimages:nn #1 #2
{
\group_begin:
\keys_set:nn { leon/loopimages } { #1 }
\dim_set:Nn \l_leon_loop_width_dim
{
(
\columnwidth -
\l_leon_loop_padding_dim * (\l_leon_loop_columns_int - 1)
) / \l_leon_loop_columns_int
}
\par % be in vertical mode
\dim_set_eq:NN \lineskip \l_leon_loop_leading_dim
\tl_use:N \l_leon_loop_align_tl % alignment
\clist_map_inline:nn { #2 }
{
\includegraphics[width=\l_leon_loop_width_dim]{\l_leon_loop_name_tl ##1}
\hspace{\l_leon_loop_padding_dim}
}
\par
\group_end:
}
\ExplSyntaxOff
\begin{document}
\begin{figure}[htp]
\loopimages[
name=image_index,
]{2,3,10,11,13,14,16,17}
\caption{Some images}
\end{figure}
\begin{figure}[htp]
\loopimages[
name=image_index,
columns=5,
last=center,
]{2,3,10,11,13,14,16,17}
\caption{Some images}
\end{figure}
\begin{figure}[htp]
\loopimages[
name=image_index,
columns=5,
last=flush,
]{2,3,10,11,13,14,16,17}
\caption{Some images}
\end{figure}
\end{document}
允许的键是:
name
对于文件名中的公共部分(这当然应该始终存在)columns
告诉我们需要多少列(默认 4)last
使用值center
或flush
(默认)来指示如何设置最后一行padding
设置列之间的间距(默认 1em)leading
设置行之间的垂直间距(默认 1ex)
强制参数\loopimages
是索引列表。
代码计算图像宽度:我们将列宽除以列数,然后减去列之间的填充。\hspace
在两幅图像之间插入一个。