在 for 迴圈中包含帶有前導零的圖形

在 for 迴圈中包含帶有前導零的圖形

我想包含檔案名稱帶有前導零的圖形。檔案名稱是:

pic_001_測試.png
pic_002_測試.png
pic_028_測試.png

我可以在 a 中輸出前導零\forloop,但無法在\includegraphics命令中使用它們:

\documentclass{article}
\usepackage{forloop}
\usepackage{graphicx}
\usepackage{fmtcount}

\begin{document}

\newcommand{\filename}{}

\newcounter{i}
\forloop{i}{1}{\value{i} < 4}{
    \begin{figure}
        \renewcommand{\filename}{pic\_\padzeroes[3]{\decimal{i}}\_test.pdf}
        \includegraphics[scale=0.6]{pic_00\arabic{i}_test.png}
        \caption{Screenshot\_\arabic{i}}
        \filename
    \end{figure}
}

\end{document}

由於“!未定義的控制序列”,以下程式碼不起作用:

\newcommand{\filename}{}
\newcounter{i}
\forloop{i}{1}{\value{i} < 4}{
    \begin{figure}
        \renewcommand{\filename}{pic\_\padzeroes[3]{\decimal{i}}\_test.pdf}
        \includegraphics[scale=0.6]{\filename}
        \caption{Screenshot\_\arabic{i}}
    \end{figure}
}

答案1

LaTeX 知道(可擴展)\two@digits這似乎足以滿足您的使用(儘管如果需要,也可以擴展):

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

\newcounter{filename}
\makeatletter
\newcommand{\twodigits}{\two@digits}
\makeatother
\begin{document}

\forloop{filename}{1}{\value{filename} < 4}{
  \begin{figure}
    \includegraphics[scale=0.6]{pic_0\twodigits{\value{filename}}_test.png}
    \caption{Screenshot\_\thefilename: pic\_0\twodigits{\value{filename}}\_test.png}
  \end{figure}
}

\end{document}

由於您在檔案名稱中使用下劃線,因此似乎進行了一些複製以避免列印和使用問題。

答案2

檔案名稱必須是可擴充的:

\renewcommand*{\filename}{%
  pic_%
  \ifnum\value{i}<100 0\fi
  \ifnum\value{i}<10 0\fi
  \number\value{i}%
  _.pdf%
}%
\includegraphics[scale=0.6]{\filename}

請注意,具有隱藏副檔名的檔案名稱的巨集必須從\includegraphics.

答案3

這也應該有效:

% in preamble:
\makeatletter
% notice this will use the value of i at the time of macro use
% not at the time of definition here
\newcommand*{\filename}{%
  pic_%
  \expandafter\@gobble\the\numexpr 1000+\value{i}%
  _.pdf%
}%
\makeatother
% in body:
\includegraphics[scale=0.6]{\filename}

假設iLaTeX 計數器的值不超過 999。

相關內容