在命令中使用計數器建立檔案名

在命令中使用計數器建立檔案名

下面的程式碼並不像我想要的那樣有效:

\documentclass{article}

\usepackage{graphicx}

\newcounter{nomfigcpt} % compteur pour compter les figures insérées dans le document
\newcommand{\nomfig}{\jobname\_fig\_\thenomfigcpt.pdf\stepcounter{nomfigcpt}}

\begin{document}

\nomfig

\nomfig

%\includegraphics[scale=1]{\nomfig}

\end{document}

正如您在 pdf 中看到的命令

\名稱

是有效的,但是當我想使用它時

\包含圖形

它不起作用,而不是檔案位於目錄中。

我想我的解決方案太天真了,我嘗試找到解決方案但沒有成功。

我使用 pdflatex -synctex=0 -shell-escape -interaction=nonstopmode %.tex

有人可以幫我嗎?

答案1

的強制參數必須\includegraphics擴展為字串,以便標識檔案名稱。諸如此類的指令\stepcounter{...}不能在該參數中使用。

一個簡單的方法是使用一個新命令:

\newcommand{\numberedimage}[1][]{%
  \includegraphics[#1]{\jobname_fig_\arabic{nomfigcpt}}%
  \stepcounter{nomfigcpt}%
}

所以你可以調用任一形式:

\numberedimage

\numberedimage[width=\textwidth]

相關內容