1

I am trying to create a loop that will print out a list of image files. All of these image files have a filename like this: fa-x.jpg (where x is a character from a - h). I've made a loop, but the counter variable is not properly being converted into a character and is causing an error. Here is the code so far:

\count255 = 97
\loop
    \subsection*{Part (\char\number\count255)}
   \includegraphics[scale=0.7]{fa-\char\number\count255.jpg}
  \hfil\break
\ifnum\count255 < 104
\advance\count255 by 1
\repeat

The error I am geting:

LaTeX Warning: File `fa-\char 97.jpg' not found on input line 77.

So, it is not converting 97 into a char because it's inside the \includegraphics argument. Keep in mind that in the above loop, the \subsection line works and prints out the characters a - h.

Ideae
  • 111
  • 2
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – cfr Jan 21 '15 at 03:45
  • 1
    May I ask why you are not using a LaTeX counter? This is obviously LaTeX rather than plain TeX, so...? – cfr Jan 21 '15 at 03:47
  • 1
    Read the wikibook for more informations. Perhaps \alph fits your job. – Symbol 1 Jan 21 '15 at 03:49
  • @Symbol1 Sorry, but I don't see how that page is supposed to help the OP solve the problem at all. Sure, it explains how to use LaTeX counters rather than TeX counts. But how does that address the expansion issues here? – cfr Jan 21 '15 at 04:11
  • After you both suggested using the LaTeX counter and \alph, I tried that, and it worked. I'll include the code in an answer in case anyone else has this problem as well. Thanks for the help guys. – Ideae Jan 21 '15 at 04:28
  • @cfr Actually I was comment on your comment. Also I guess \alph is OP's final goal. Nevertheless If expansion-analysis is welcomed, I will post an answer in minutes. – Symbol 1 Jan 21 '15 at 05:32

2 Answers2

3

Since you claimed that you solved your problem, I am here to respond to cfr's comment. Some explanation follows:

  • According to The TeXBook, \char is used to print the desired character. So TeX would treat \char as \ c h a r when {fa-\char97.jpg} is passed as an input. (But it printed an "a" for \subsection, didn't it?)
  • dealing with a count/counter by index is dangerous. Since there are only 256 choices, you will never know whether that count/counter is free or not. In fact, \count255 is reserved for output routine. And even \count123 is much safer. (But still dangerous.)

Go back to your problem, if \char does not work,

How \alph works?

Well, it is indeed a hard work

\ifcase #1\or a\or b\or c\or d\or e\or f\or g\or h\or i\or j\or k\or l\or m\or n
\or o\or p\or q\or r\or s\or t\or u\or v\or w\or x\or y\or z\else \@ctrerr \fi

Any Alternatives?

Sure, write ^^61 for letter "a" in an input. (Notice that HEX61 = DEC97.) But ^^\value{count} does not work. It is actually ^^\ plus value{count}. Neither \expandafter^\expandafter^\value{a} works.

What if I need ^^\value{count}?

At least you can print whatever you want to a file and \input it latter. I do not know if there exists a direct way.

Symbol 1
  • 36,855
0

The answer was to use the LaTeX counter rather than a TeX counter. Here is the code that made it work:

\documentclass[11pt, fleqn]{article}
\usepackage{graphicx}
\begin{document}

\newcounter{count}
\setcounter{count}{1}
\loop\ifnum\value{count} < 8
    \subsection*{Part (\alph{count})}
   \includegraphics[scale=0.7]{fa-\alph{count}.jpg}
  \hfil\break
\stepcounter{count}
\repeat

\end{document}

The \alph{counter} function is usable on counters, which i used to inject the character into the filename. If counter is at 1, alph{counter} will return 'a'.

Ideae
  • 111
  • 2