
我正在編寫一個文件來生成一副紙牌。卡片的值儲存在一個通用列表中。每個卡片顏色/套裝都有一組自己的圖形文件,也在通用列表中定義。即,值和檔案名稱是任意的,但為了偵錯目的,檔案名稱在下面的程式碼中系統化。
這個想法是在 for 循環中運行索引,並為循環中的每張卡產生值和圖形檔案名稱。運行索引可以超出任一列表的長度以產生更多卡片,因此使用 MyModulo 函數。
使用 GetListMember 函數/巨集產生文字輸出,它可以正確產生檔案名稱。
當使用 GetListMember 函數/巨集與 includegraphis 作為檔名時,編譯失敗:
!不完整 \iffalse;第 64 行後所有文字都被
忽略
。
我不明白為什麼。
我真的想要一個通用的解決方案,其中文件名可以是任意的。 (不同列表的長度是素數,因此帶有連音的解決方案可能不是最有效的。)
\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}