I am writing a document to generate a deck of cards. The values of the cards is stored in a general list. Each card color/suit has a own set of graphic files, also defined in general lists. I.e. the values and file names are arbitary, but for debugging purpose the file names are systemized in the code below.
The idea is to run an index in a for loop and generate both value and graphic file name for each card in the loop. The running index can go further than the length of either list to generate more cards, hence the use of MyModulo function.
Using the GetListMember function/macro to generate text output, it produces the file names correctly.
When using the GetListMember function/macro together with includegraphis for the file names, the compilation fails:
! Incomplete \iffalse; all text was ignored after line 64.
inserted text
\fi
and I can not figure out why.
I really would like to have a generalized solution, where the file names can be arbitary. (The length of the different lists are prime numbers, hence is a solution with tuplets probably not the most effective.)
\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}
\intcalcIncinside an\edefbut this macro is not expandable. – Martin Scharrer Dec 16 '17 at 13:40