图像 for 循环中出现“缺少 \endcsname 插入”错误

图像 for 循环中出现“缺少 \endcsname 插入”错误

我有一个 for 循环,它遍历与图像相关的已定义名称的列表:

\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}
        }

它只编译了少数图像,并出现了一些错误,提示“缺少插入的 \endcsname”,但由于我添加了完整的图像列表,因此由于收到的错误数量太多,它甚至无法编译。

答案1

下面通过添加一个函数来修改您的循环,该函数将标题更改_为并从图像名称中删除下划线。我还放弃了环境的使用,因为如果您使用如此多的图片,这没有意义(并且会导致错误)。相反,下面使用s 表示图形和包提供的。请注意,如果您使用包或 KOMA 脚本类,则无需加载for 。\_figureminipage\captionofcapt-ofcaptioncapt-of\captionof

另外:永远不要让错误堆积如山。如果您的文档出现一个错误,您就不能依赖文档的任何输出。一旦出现错误,请立即修复。Overleaf 会继续编译,但这只是为了让您以后能够看到其他错误(即使对于那些您不能依赖它们是正确的错误,它们也可能是由另一个早期错误引起的)。切勿使用失败的 LaTeX 运行的输出!

\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}

答案2

您不应该\_在文件名中使用,但是_

这是一种在标题中使用带有下划线的文件名的方法,也是在列表上进行循环的不同版本。

代码中大部分行都被注释掉了(只需要几张图来展示其工作原理)。

在第二个参数中\uselist放置要重复的代码,其中#1代表循环中的当前项目。

\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}

假设每张图都要打印在一页上。否则,\clearpage您将遇到“未处理的浮动过多”错误。

使用该draft选项是因为我没有命名的文件。

在此处输入图片描述

相关内容