0

I have a for loop that goes over a list of defined names associated with images:

\def \names{{3088452\_cutouts.png},{3088553\_cutouts.png},{3089892\_cutouts.png},{3090579\_cutouts.png},{3090786\_cutouts.png},{3093042\_cutouts.png},{3093066\_cutouts.png},{3094512\_cutouts.png},{3094995\_cutouts.png},{3097355\_cutouts.png},{3097545\_cutouts.png},{3097836\_cutouts.png},{3098314\_cutouts.png},{3098825\_cutouts.png},{3098856\_cutouts.png},{3099316\_cutouts.png},{3099655\_cutouts.png},{3099666\_cutouts.png},{3099797\_cutouts.png},{3100022\_cutouts.png},{3103195\_cutouts.png},{3106872\_cutouts.png},{3107048\_cutouts.png},{3109542\_cutouts.png},{3110179\_cutouts.png},{3111074\_cutouts.png},{3111356\_cutouts.png},{3111453\_cutouts.png},{3111827\_cutouts.png},{3112137\_cutouts.png},{3112834\_cutouts.png},{3112836\_cutouts.png},{3115668\_cutouts.png},{3116242\_cutouts.png},{3116431\_cutouts.png},{3120222\_cutouts.png},{3120350\_cutouts.png},{3120942\_cutouts.png},{3122269\_cutouts.png},{3122654\_cutouts.png},{3123413\_cutouts.png},{3124882\_cutouts.png},{3125668\_cutouts.png},{3125782\_cutouts.png},{3125949\_cutouts.png},{3126942\_cutouts.png},{3127080\_cutouts.png},{3127173\_cutouts.png},{3129956\_cutouts.png},{3130523\_cutouts.png},{3135948\_cutouts.png},{3135979\_cutouts.png},{3138157\_cutouts.png},{3138718\_cutouts.png},{3138924\_cutouts.png},{3139962\_cutouts.png},{3140630\_cutouts.png},{3141006\_cutouts.png},{3141053\_cutouts.png},{3141144\_cutouts.png},{3144020\_cutouts.png},{3144399\_cutouts.png},{3144504\_cutouts.png},{3144525\_cutouts.png},{3145019\_cutouts.png},{3145452\_cutouts.png},{3147751\_cutouts.png},{3148053\_cutouts.png},{3151152\_cutouts.png}}
        \foreach \name in \names{%
            \begin{figure}
                \centering
                \includegraphics[width=15cm]{\name}
                \caption{\name}
                \label{\name}
            \end{figure}
        }

With only a few of the images it compiled with a few errors saying the 'Missing \endcsname inserted', but since I have added the full list of images it won't even compile due to the amount of errors I get.

4

2 Answers 2

0

The following modifies your loop by adding a function that'll change _ to \_ for the captions and removing the underscore from the image names. I've also dropped the usage of the figure environment, as that doesn't make sense if you use so many pictures (and will result in an error). Instead the following uses minipages for the figures and \captionof provided by the capt-of package. Please note that if you use the caption package or a KOMA-script class you don't need to load capt-of for \captionof.

Also: Never let errors pile up. If your document throws a single error you can't rely on any of the output of your document. As soon as there is an error, fix it. Overleaf keeps compiling, but that's only so you can see other errors down the road (and even for those you can't rely on them being correct, they might just be caused by another earlier error). Never use the output of a failed LaTeX run!

\documentclass[]{article}

\usepackage{capt-of}
\usepackage{pgffor}
\usepackage[demo]{graphicx}

\ExplSyntaxOn
\NewDocumentCommand \CTunnelRageReplace { s m m m }
  {
    \group_begin:
      \IfBooleanTF {#1}
        \tl_set:No
        \tl_set:Nn
        \l_tmpa_tl {#2}
      \tl_replace_all:Nnn \l_tmpa_tl {#3} {#4}
      \tl_use:N \l_tmpa_tl
    \group_end:
  }
\ExplSyntaxOff

\begin{document}
\def \names{{3088452_cutouts.png},{3088553_cutouts.png},{3089892_cutouts.png},{3090579_cutouts.png},{3090786_cutouts.png},{3093042_cutouts.png},{3093066_cutouts.png},{3094512_cutouts.png},{3094995_cutouts.png},{3097355_cutouts.png},{3097545_cutouts.png},{3097836_cutouts.png},{3098314_cutouts.png},{3098825_cutouts.png},{3098856_cutouts.png},{3099316_cutouts.png},{3099655_cutouts.png},{3099666_cutouts.png},{3099797_cutouts.png},{3100022_cutouts.png},{3103195_cutouts.png},{3106872_cutouts.png},{3107048_cutouts.png},{3109542_cutouts.png},{3110179_cutouts.png},{3111074_cutouts.png},{3111356_cutouts.png},{3111453_cutouts.png},{3111827_cutouts.png},{3112137_cutouts.png},{3112834_cutouts.png},{3112836_cutouts.png},{3115668_cutouts.png},{3116242_cutouts.png},{3116431_cutouts.png},{3120222_cutouts.png},{3120350_cutouts.png},{3120942_cutouts.png},{3122269_cutouts.png},{3122654_cutouts.png},{3123413_cutouts.png},{3124882_cutouts.png},{3125668_cutouts.png},{3125782_cutouts.png},{3125949_cutouts.png},{3126942_cutouts.png},{3127080_cutouts.png},{3127173_cutouts.png},{3129956_cutouts.png},{3130523_cutouts.png},{3135948_cutouts.png},{3135979_cutouts.png},{3138157_cutouts.png},{3138718_cutouts.png},{3138924_cutouts.png},{3139962_cutouts.png},{3140630_cutouts.png},{3141006_cutouts.png},{3141053_cutouts.png},{3141144_cutouts.png},{3144020_cutouts.png},{3144399_cutouts.png},{3144504_cutouts.png},{3144525_cutouts.png},{3145019_cutouts.png},{3145452_cutouts.png},{3147751_cutouts.png},{3148053_cutouts.png},{3151152_cutouts.png}}
        \foreach \name in \names{%
          \par
          \vskip\floatsep
          \noindent
          \begin{minipage}{\textwidth}%
                \centering
                \includegraphics[width=\linewidth]{\name}
                \captionof{figure}
                  {\CTunnelRageReplace*\name{_}{\_}\label{\name}}
            \end{minipage}%
          \par
          \vskip\floatsep
        }
\end{document}
0

You should not use \_ in file names, but _.

Here's a way to use the file name with the underscore in the caption and also a different version for doing a loop on the list.

In the code most of the lines are commented out (only a few figures are necessary to show the working).

In the second argument to \uselist you place the code to be repeated, where #1 stands for the current item in the loop.

\documentclass{article}
\usepackage[draft]{graphicx}

\ExplSyntaxOn

\NewDocumentCommand{\makelist}{m m}
 {% #1 = list name, #2 = items
  \clist_clear_new:c { l_ctunnelrage_#1_clist }
  \clist_set:cn { l_ctunnelrage_#1_clist } { #2 }
 }

\NewDocumentCommand{\uselist}{m +m}
 {% #1 = list name, #2 = code to use
  \clist_map_inline:cn { l_ctunnelrage_#1_clist } { #2 }
 }

\NewDocumentCommand{\replace}{m m m}
 {
  \tl_set:Nn \l__ctunnelrage_temp_tl { #1 }
  \tl_replace_all:Nnn \l__ctunnelrage_temp_tl { #2 } { #3 }
  \tl_use:N \l__ctunnelrage_temp_tl
 }

\tl_new:N \l__ctunnelrage_temp_tl

\ExplSyntaxOff

\makelist{names}{
  3088452_cutouts.png,3088553_cutouts.png,3089892_cutouts.png,
%  3090579_cutouts.png,3090786_cutouts.png,3093042_cutouts.png,
%  3093066_cutouts.png,3094512_cutouts.png,3094995_cutouts.png,
%  3097355_cutouts.png,3097545_cutouts.png,3097836_cutouts.png,
%  3098314_cutouts.png,3098825_cutouts.png,3098856_cutouts.png,
%  3099316_cutouts.png,3099655_cutouts.png,3099666_cutouts.png,
%  3099797_cutouts.png,3100022_cutouts.png,3103195_cutouts.png,
%  3106872_cutouts.png,3107048_cutouts.png,3109542_cutouts.png,
%  3110179_cutouts.png,3111074_cutouts.png,3111356_cutouts.png,
%  3111453_cutouts.png,3111827_cutouts.png,3112137_cutouts.png,
%  3112834_cutouts.png,3112836_cutouts.png,3115668_cutouts.png,
%  3116242_cutouts.png,3116431_cutouts.png,3120222_cutouts.png,
%  3120350_cutouts.png,3120942_cutouts.png,3122269_cutouts.png,
%  3122654_cutouts.png,3123413_cutouts.png,3124882_cutouts.png,
%  3125668_cutouts.png,3125782_cutouts.png,3125949_cutouts.png,
%  3126942_cutouts.png,3127080_cutouts.png,3127173_cutouts.png,
%  3129956_cutouts.png,3130523_cutouts.png,3135948_cutouts.png,
%  3135979_cutouts.png,3138157_cutouts.png,3138718_cutouts.png,
%  3138924_cutouts.png,3139962_cutouts.png,3140630_cutouts.png,
%  3141006_cutouts.png,3141053_cutouts.png,3141144_cutouts.png,
%  3144020_cutouts.png,3144399_cutouts.png,3144504_cutouts.png,
%  3144525_cutouts.png,3145019_cutouts.png,3145452_cutouts.png,
%  3147751_cutouts.png,3148053_cutouts.png,3151152_cutouts.png
}

\begin{document}

\uselist{names}{
  \begin{figure}[p]
    \centering
    \includegraphics[width=\textwidth]{#1}
    \caption{\replace{#1}{_}{\_}}
    \label{#1}
  \end{figure}
  \clearpage
}

\end{document}

It is assumed that the figures are to be printed on a page each. Without \clearpage you'd incur in the “Too many unprocessed floats” error.

The draft option is used because I don't have the named files.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .