![Latex中使用For循環輸入不同目錄下的多個文件](https://rvso.com/image/328816/Latex%E4%B8%AD%E4%BD%BF%E7%94%A8For%E5%BE%AA%E7%92%B0%E8%BC%B8%E5%85%A5%E4%B8%8D%E5%90%8C%E7%9B%AE%E9%8C%84%E4%B8%8B%E7%9A%84%E5%A4%9A%E5%80%8B%E6%96%87%E4%BB%B6.png)
\documentclass{minimal}
\usepackage{chngcntr}
\usepackage{pgffor}
\newcounter{question}
\newcounter{answer}
\setcounter{question}{0}
\setcounter{answer}{0}
\newcommand\Que[1]{
\stepcounter{question}}
\newcommand\Ans[2][]{
\stepcounter{answer}}
\newcommand\MAX{10}
\begin{document}
\foreach \i in {1,...,\MAX} {%
\edef\File{ques\i}% The % here are necessary to eliminate any
\edef\FileName{ans\i}% The % here are necessary to eliminate any
\IfFileExists{\FileName}{% spurious spaces that may get inserted
\input{\File}% at these points
\input{\FileName}% at these points
}
}
\end{document}
這是我的主乳膠檔案的程式碼,我還有另外兩個名為「問題」和「答案」的目錄。.tex
問題目錄中大約有六個名為ques1
, ... 的問題文件,而答案目錄中名為, ,...ques2
的文件數量相同,我想在上面的for 循環中指定目錄,以便它自動從上述文件夾中獲取文件這樣我就不必將它們保存在一個資料夾中。還有一種方法可以讓我省略提供 i 的值,而是提供一些可以讀取資料夾中所有檔案的其他條件。.tex
ans1
ans2
.tex