我正在使用 animate 包,并将要制作动画的 pdf 文件放在单独的文件夹中。我需要输入第一个和最后一个文件的数量,但偶尔会更改要制作动画的文件数量。有没有办法让 LaTex 自动计算文件夹中的文件数量?
下面是一些示例代码,希望能更好地解释我所追求的内容
\documentclass{article}
\usepackage{animate}
%
\begin{document}
\animategraphics[controls]{1}{./pdfFolder/fileName_}{1}{numFiles}
\end{document}
我希望 LaTex 根据“pdfFolder”中的文件数量自动更新“numFiles”。
预先感谢您的帮助!
答案1
纯 LaTeX:(fileName_1.pdf ... fileName_?.pdf
)
\documentclass{article}
\usepackage{animate}
\usepackage{graphicx}
\newcounter{NumFiles}
%\setcounter{NumFiles}{-1} % fileName_0.pdf ... fileName_?.pdf
\newboolean{stop}
\whiledo{\NOT\boolean{stop}}{
\stepcounter{NumFiles}
\IfFileExists{./pdfFolder/fileName_\theNumFiles.pdf}{}{
\addtocounter{NumFiles}{-1}
\setboolean{stop}{true}
}
}
\begin{document}
\animategraphics[controls]{1}{./pdfFolder/fileName_}{1}{\theNumFiles}
\end{document}
答案2
这是一种使用基本 Unix 命令和的方法\write18{}
,首先列出内容然后使用wc -l
命令,将输出保存到 dummyvar.tex 并将该文件读入\numFiles
宏。
当然,shell 脚本应该更加安全,即将其存储在单独的文件中,而不是countmyfiles.sh
在其中获取该文件\write18{source countmyfiles.sh}
。
\documentclass{article}
\usepackage{animate}
\def\numFiles{-1}
\newread\numfileshandle
\begin{document}
\write18{rm -f dummyvar.tex ; ls -l pdfFolder/fileName_* | wc -l > ./dummyvar.tex}
\immediate\openin\numfileshandle=dummyvar.tex
\read\numfileshandle to \numFiles
\immediate\closein\numfileshandle
There are \numFiles Files
%\animategraphics[controls]{1}{./pdfFolder/fileName_}{1}{\numFiles}
\end{document}
使用我的虚拟文件夹 pdfFolder 中的 20 个虚拟文件进行测试,得到 20 个输出。
答案3
Christian 拒绝了我关于使用管道输入功能的建议,但我仍然认为展示一下在这种情况下如何使用它可能会很有趣。请注意宏的参数\countfiles
是如何预处理的,以便允许使用“特殊”字符,如 _
和均衡{
和 的出现}
,同时允许宏(或更一般地,可扩展的控制序列)进行扩展,如下所示的\myDirName
和\jobname
。
% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly
% declare the paper format.
\usepackage[T1]{fontenc} % Not always necessary, but recommended.
% End of standard header. What follows pertains to the problem at hand.
\newcounter{numberOfFiles}
\makeatletter
% Beware: "special" characters like "_" that appear in the directory name must
% be either re-"\catcode"d or "\detokenize"d: the latter method is **much**
% simpler than the former, but it does **not** cope with filenames containing
% the "%" character, or unbalanced "{" and "}" characters, and in addition it
% requires e-TeX extensions (not at all a concern, these days!).
\newcommand*{\countfiles}[1]{%
\begingroup
\protected@edef\@tempa{#1}% before detokenizing, expand expandable (and
% unprotected) tokens
% We use "\@inputcheck" for the temporary input pipe:
\openin\@inputcheck "|ls -1 % the last character is the digit "one"
\expandafter\detokenize\expandafter{\@tempa} % space meant
|wc -l% the last character is the letter "ell"
"\relax % without the "\relax", the ensuing "\ifeof" would be
% prematurely expanded
\ifeof\@inputcheck
% An error message could be triggered here.
\typeout{WARNING: Could not read \protect\numFiles!}%
\else
\endlinechar \m@ne % cf. exercise 20.18
\readline\@inputcheck to\@tempa
\setcounter{numberOfFiles}{\@tempa}%
\fi
\closein\@inputcheck % note: no "\immediate" needed for "\closein"
\endgroup
}
\makeatother
\newcommand*{\myDirName}{some_directory}
\begin{document}
\setcounter{numberOfFiles}{-1}\countfiles{\myDirName}
There are \arabic{numberOfFiles} files in the specified directory.
\bigbreak
\setcounter{numberOfFiles}{-1}
In the current directory there are:
\begin{tabbing}
\quad 999\=\kill
\countfiles{\jobname.*}\>\arabic{numberOfFiles}\'
file(s) whose basename is \texttt{\jobname};\\
\countfiles{*.{tex,log}}\>\arabic{numberOfFiles}\'
file(s) whose extension is either \texttt{.tex} or \texttt{.log};\\
\countfiles{[A-Za-z]*.tex}\>\arabic{numberOfFiles}\'
file(s) whose name consists only of letters, followed by the
extension \texttt{.tex}.
\end{tabbing}
That's all, folks!
\end{document}