1

I have a list of figures which I insert into my document using a "foreach" loop. Is there a way to also generate labels for each of these figures from within the foreach loop? I have the following code but running it produces captions, but no labels!

My example is as follows:

       \foreach \x in \imageNames
{
    \begin{figure}[H]
    \centering
        \includegraphics[max size={\textwidth}{\textheight}]{figures/TGA-DTG/\x.png}
        \caption{\x}
        \foreach \y in {1,2, 3,...,64}
        {\label{fig:DTG\y}}
    \end{figure}
}

where the preamble has:

    \newcommand*{\imageNames}
{AFCO 01-JAN-19,AFCO 02-NOV-18,AFCO 03-APR-19,AFCO 03-JAN-19,%
AFCO 03-OCT-18,AFCO 05-DEC-18,AFCO 05-JUL-18,AFCO 05-MAR-19,% 
AFCO 05-NOV-18,AFCO 06-FEB-19,AFCO 09-JAN-19,AFCO 12-DEC-18,% 
AFCO 13-DEC-18,AFCO 14-MAR-19,AFCO 14-OCT-18,AFCO 17-DEC-18,% 
AFCO 24-NOV-18,AFCO 27-DEC-18,AFCO 27-NOV-18,AFCO 28-FEB-19,% 
AFCO 30-DEC-18,AFCO 30-JAN-19,AUS 100,BLUESCOPE,BUSTY BLEND,BUSTY FINES,%
BUSTY IN BASE,COKE BREEZE BLEND,COLOMBIAN,FYF,IJmuiden 50CRI,MORFA 02-FEB-21,%
MORFA 14-JAN-21,MORFA 16-JAN-21,MORFA 18-JAN-21,MORFA 21-JAN-21,MORFA 29-JAN-21,% 
MORFA FLOAT 02-FEB-21,MORFA FLOAT 23-02-21,MORFA FLOAT 3-FEB-21,% 
MORFA FLOAT 27-JAN-21,MORFA FLOAT 28-JAN-21,MORFA FLOAT 30-JAN-21,% 
MORFA GROUND 1,MORFA GROUND 2,MORFA GROUND 3,MORFA GROUND 4,% 
MORFA GROUND 5,MORFA GROUND 6,MORFA GROUND 7,MORFA SINK 23-FEB-21,% 
MORFA SINK 24-FEB-21,MORFA SINK 27-JAN-21,MORFA SINK 28-JAN-21,%
MORFA SINK 30-JAN-21,MORFA WK 1,MORFA WK 39,MORFA WK 4,MORFA WK 8,POLISH 100,%
REF 3 BLEND 2,RISUN 10-JUL-18,RISUN ERINI P,US LV 50-50,US MV}
4
  • 1
    \caption has to be in a figure or use \captionof{figure}{...} from capt-of package (don't use \\ before the caption) Aug 16 at 15:30
  • A caption needs a figure (or table) environment, add \begin{figure} and \end{figure} within the loop. This will probably completely mess up your layout so it would be easier not to use \caption at all and just print \x after \includegraphics.
    – Marijn
    Aug 16 at 15:30
  • 1
    Hi, welcome to TeX.SE. You can use multiple variables in \foreach. Here's documentation.
    – Celdor
    Aug 16 at 15:31
  • thanks @DavidCarlisle, I've updated the code to include your suggestions. I'm now struggling to auto-label them!
    – Gokotai
    Aug 16 at 16:10

2 Answers 2

3

I first tried using a TikZ array, but without quotes it was turned into a number, and with quotes it wasn't treated as an array.

Instead I tried xstring. I got it to work, but it isn't pretty.

Oh, and LaTeX does not like having that many floats in the queue.

\documentclass[draft]{article} 
\usepackage[export]{adjustbox}
\usepackage{tikz}
\usepackage{xstring}

  \newcommand*{\imageNames}
{,AFCO 01-JAN-19,AFCO 02-NOV-18,AFCO 03-APR-19,AFCO 03-JAN-19,%
AFCO 03-OCT-18,AFCO 05-DEC-18,AFCO 05-JUL-18,AFCO 05-MAR-19,% 
AFCO 05-NOV-18,AFCO 06-FEB-19,AFCO 09-JAN-19,AFCO 12-DEC-18,% 
AFCO 13-DEC-18,AFCO 14-MAR-19,AFCO 14-OCT-18,AFCO 17-DEC-18,% 
AFCO 24-NOV-18,AFCO 27-DEC-18,AFCO 27-NOV-18,AFCO 28-FEB-19,% 
AFCO 30-DEC-18,AFCO 30-JAN-19,AUS 100,BLUESCOPE,BUSTY BLEND,BUSTY FINES,%
BUSTY IN BASE,COKE BREEZE BLEND,COLOMBIAN,FYF,IJmuiden 50CRI,MORFA 02-FEB-21,%
MORFA 14-JAN-21,MORFA 16-JAN-21,MORFA 18-JAN-21,MORFA 21-JAN-21,MORFA 29-JAN-21,% 
MORFA FLOAT 02-FEB-21,MORFA FLOAT 23-02-21,MORFA FLOAT 3-FEB-21,% 
MORFA FLOAT 27-JAN-21,MORFA FLOAT 28-JAN-21,MORFA FLOAT 30-JAN-21,% 
MORFA GROUND 1,MORFA GROUND 2,MORFA GROUND 3,MORFA GROUND 4,% 
MORFA GROUND 5,MORFA GROUND 6,MORFA GROUND 7,MORFA SINK 23-FEB-21,% 
MORFA SINK 24-FEB-21,MORFA SINK 27-JAN-21,MORFA SINK 28-JAN-21,%
MORFA SINK 30-JAN-21,MORFA WK 1,MORFA WK 39,MORFA WK 4,MORFA WK 8,POLISH 100,%
REF 3 BLEND 2,RISUN 10-JUL-18,RISUN ERINI P,US LV 50-50,US MV,}

\begin{document}

\foreach \y in {1,...,32}% too many will crash
{
    \begin{figure}[p]% only [p] can fill an entire page
    \centering
        \count1=\y\relax
        \count2=\numexpr \y+1\relax
        \StrBetween[\count1,\count2]{\imageNames}{,}{,}[\x]%
        \includegraphics[max size={\textwidth}{\textheight}]{figures/TGA-DTG/\x.png}
        \caption{\x}
        \label{fig:DTG\y}
    \end{figure}
}
\end{document}

Just for fun, this uses a \csname array.

Note that the only way to include a numeral in a macro name is by using \csname ... \endcsname.

\documentclass[draft]{article} 
\usepackage[export]{adjustbox}
\usepackage{tikz}

  \newcommand*{\imageNames}
{AFCO 01-JAN-19,AFCO 02-NOV-18,AFCO 03-APR-19,AFCO 03-JAN-19,%
AFCO 03-OCT-18,AFCO 05-DEC-18,AFCO 05-JUL-18,AFCO 05-MAR-19,% 
AFCO 05-NOV-18,AFCO 06-FEB-19,AFCO 09-JAN-19,AFCO 12-DEC-18,% 
AFCO 13-DEC-18,AFCO 14-MAR-19,AFCO 14-OCT-18,AFCO 17-DEC-18,% 
AFCO 24-NOV-18,AFCO 27-DEC-18,AFCO 27-NOV-18,AFCO 28-FEB-19,% 
AFCO 30-DEC-18,AFCO 30-JAN-19,AUS 100,BLUESCOPE,BUSTY BLEND,BUSTY FINES,%
BUSTY IN BASE,COKE BREEZE BLEND,COLOMBIAN,FYF,IJmuiden 50CRI,MORFA 02-FEB-21,%
MORFA 14-JAN-21,MORFA 16-JAN-21,MORFA 18-JAN-21,MORFA 21-JAN-21,MORFA 29-JAN-21,% 
MORFA FLOAT 02-FEB-21,MORFA FLOAT 23-02-21,MORFA FLOAT 3-FEB-21,% 
MORFA FLOAT 27-JAN-21,MORFA FLOAT 28-JAN-21,MORFA FLOAT 30-JAN-21,% 
MORFA GROUND 1,MORFA GROUND 2,MORFA GROUND 3,MORFA GROUND 4,% 
MORFA GROUND 5,MORFA GROUND 6,MORFA GROUND 7,MORFA SINK 23-FEB-21,% 
MORFA SINK 24-FEB-21,MORFA SINK 27-JAN-21,MORFA SINK 28-JAN-21,%
MORFA SINK 30-JAN-21,MORFA WK 1,MORFA WK 39,MORFA WK 4,MORFA WK 8,POLISH 100,%
REF 3 BLEND 2,RISUN 10-JUL-18,RISUN ERINI P,US LV 50-50,US MV}

\newcounter{index}
\foreach \x in \imageNames {% create \imageName1, \imageName2. ...
  \stepcounter{index}%
  \expandafter\xdef\csname imageName\theindex\endcsname{\x}%
}

\begin{document}
\listoffigures

\foreach \y in {1,...,32}% too many will crash
{
    \begin{figure}[p]% only [p] can fill an entire page
    \centering
        \includegraphics[max size={\textwidth}{\textheight}]{figures/TGA-DTG/\csname imageName\y\endcsname.png}
        \caption{\csname imageName\y\endcsname}
        \label{fig:DTG\y}
    \end{figure}
}

\end{document}
6
  • Thanks for this! Unfortunately, I don't think the auto-labels are working as when I try to reference a single plot, typing \ref{fig:} only brings up "DTG\y"...is this intended?
    – Gokotai
    Aug 17 at 8:33
  • fixed by changing it to: \caption{\x \label{fig:DTG\y}}
    – Gokotai
    Aug 17 at 15:33
  • Interesting! It shouldn't make a difference as long as \caption and \label are in the same group (in this case, figure). Aug 17 at 15:51
  • apologies, I think I spoke too soon! It unfortunately didn't change the outcome, the labelling still doesn't work!
    – Gokotai
    Aug 17 at 15:55
  • I get \ref{fig:DTG1} to return the caption number (1). What were you expecting? Aug 17 at 15:59
1

You are using \foreach from the PGF/TikZ codebase which means that you can use the count optional extra to keep track of how many images have been processed.

\documentclass[draft]{article}
%\url{https://tex.stackexchange.com/q/654216/86}
\usepackage{lipsum}
\usepackage{pgffor}

\newcommand*{\imageNames}
{AFCO 01-JAN-19,AFCO 02-NOV-18,AFCO 03-APR-19,AFCO 03-JAN-19,%
AFCO 03-OCT-18,AFCO 05-DEC-18,AFCO 05-JUL-18,AFCO 05-MAR-19,% 
AFCO 05-NOV-18,AFCO 06-FEB-19,AFCO 09-JAN-19,AFCO 12-DEC-18,% 
AFCO 13-DEC-18,AFCO 14-MAR-19,AFCO 14-OCT-18,AFCO 17-DEC-18,% 
AFCO 24-NOV-18,AFCO 27-DEC-18,AFCO 27-NOV-18,AFCO 28-FEB-19,% 
AFCO 30-DEC-18,AFCO 30-JAN-19,AUS 100,BLUESCOPE,BUSTY BLEND,BUSTY FINES,%
BUSTY IN BASE,COKE BREEZE BLEND,COLOMBIAN,FYF,IJmuiden 50CRI,MORFA 02-FEB-21,%
MORFA 14-JAN-21,MORFA 16-JAN-21,MORFA 18-JAN-21,MORFA 21-JAN-21,MORFA 29-JAN-21,% 
MORFA FLOAT 02-FEB-21,MORFA FLOAT 23-02-21,MORFA FLOAT 3-FEB-21,% 
MORFA FLOAT 27-JAN-21,MORFA FLOAT 28-JAN-21,MORFA FLOAT 30-JAN-21,% 
MORFA GROUND 1,MORFA GROUND 2,MORFA GROUND 3,MORFA GROUND 4,% 
MORFA GROUND 5,MORFA GROUND 6,MORFA GROUND 7,MORFA SINK 23-FEB-21,% 
MORFA SINK 24-FEB-21,MORFA SINK 27-JAN-21,MORFA SINK 28-JAN-21,%
MORFA SINK 30-JAN-21,MORFA WK 1,MORFA WK 39,MORFA WK 4,MORFA WK 8,POLISH 100,%
REF 3 BLEND 2,RISUN 10-JUL-18,RISUN ERINI P,US LV 50-50,US MV}

\begin{document}

\foreach[count=\y] \x in \imageNames
{
  \begin{figure}[p]
  \centering
  Float: \x, Figure: \y
%  \includegraphics[max size={\textwidth}{\textheight}]{figures/TGA-DTG/\x.png}
  \caption{\x}
  \label{fig:DTG\y}
  \end{figure}

  \lipsum[1]
}


\end{document}

I've added in a dummy paragraph (from the lipsum package) between each figure to avoid the "Too many floats" issue. I figure that that is a side issue to what you are trying to solve here. Also, as I don't have the actual images then I commented out the \includegraphics.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.