我正在编写一个文档来生成一副牌。牌的值存储在一个通用列表中。每种牌的颜色/花色都有一组自己的图形文件,也在通用列表中定义。即值和文件名是任意的,但出于调试目的,文件名在下面的代码中是系统化的。
这个想法是在 for 循环中运行索引,并在循环中为每张卡片生成值和图形文件名。运行索引可以超出任一列表的长度以生成更多卡片,因此使用 MyModulo 函数。
使用 GetListMember 函数/宏生成文本输出,它可以正确生成文件名。
当将 GetListMember 函数/宏与 includegraphis 一起用于文件名时,编译失败:
! 不完整 \iffalse;第 64 行之后的所有文本均被忽略。
插入的文本
\fi
我不知道为什么。
我真的很想有一个通用的解决方案,其中文件名可以是任意的。(不同列表的长度是素数,因此使用连字符的解决方案可能不是最有效的。)
\documentclass{article}
\usepackage{ifthen}
\usepackage{pgffor}
\usepackage{graphicx}
\usepackage{intcalc}
\usepackage{xstring}
\graphicspath{{../images/}}
% Define card values and graphics
\newcommand{\MyValues}{10,Kn,D,K,E}% Values of the cards
\newcommand{\MyValuesN}{5}
\newcommand{\MyCard}{temp1,temp2,temp3}% Card images in one color
\newcommand{\MyCardN}{3}
\newcommand{\MyCardA}{tempA,tempB,tempC}% Card images in second color
\newcommand{\MyCardAN}{3}
% Define wrap around index function.
\newcommand{\MyModulo}[2]{%
\intcalcInc{\intcalcMod{\intcalcDec{#1}}{#2}}%
}
% From http://tex.stackexchange.com/questions/21559/macro-to-access-a-specific-member-of-a-list
% This works both with inline lists and with macros containing lists
\newcommand*{\GetListMember}[2]{%
\edef\dotheloop{%
\noexpand\foreach \noexpand\a [count=\noexpand\i] in {#1} {%
\noexpand\IfEq{\noexpand\i}{#2}{\noexpand\a\noexpand\breakforeach}{}%
}}%
\dotheloop%
}%
%Stand in function generating the cards, the real function is more elaborated.
\newcommand{\docard}[2]{%
!#1! % Value
\includegraphics[width=2cm]{#2} %graphics
}
\begin{document}
\foreach \n in {1,...,7} {%
%
%This works, shows the values.
%\GetListMember{\MyValues}{\MyModulo{\n}{\MyValuesN}}
%
%This works, produces the correct image file names.
%\GetListMember{\MyCard}{\MyModulo{\n}{\MyCardN}}.png
%
%This works, includes the images.
%\includegraphics[width=2cm]{temp\MyModulo{\n}{\MyCardN}.png}
%
%This does not work.
%\includegraphics[width=2cm]{\GetListMember{\MyCard}{\MyModulo{\n}{\MyCardN}}.png}
%
%This works, calls the card macro correctly.
%\docard{\GetListMember{\MyValues}{\MyModulo{\n}{\MyValuesN}}}{temp\MyModulo{\n}{\MyCardN}.png}
%
%This does not work.
\docard{\GetListMember{\MyValues}{\MyModulo{\n}{\MyValuesN}}}
{\GetListMember{\MyCard}{\MyModulo{\n}{\MyCardN}}.png}
\par
}
% Second color of cards
%\foreach \n in {1,...,7} {
% \docard{\GetListMember{\MyValues}{\MyModulo{\n}{\MyValuesN}}}
% {\GetListMember{\MyCardA}{\MyModulo{\n}{\MyCardNA}}.png}
% \par
%}
% Third colors of cards
%\foreach ...
\end{document}