如何在\setlength中使用\input?

如何在\setlength中使用\input?

我正在嘗試這個:

\documentclass{article}
\usepackage{calc}
\newlength{\foo}
\newcommand{\zzz}{\input{size.txt}}
\setlength{\foo}{2in * \zzz}
\usepackage[paperwidth=\foo,paperheight=\foo]{geometry}
\begin{document}
\end{document}

但我得到:

! Missing number, treated as zero.
<to be read again>
                   \let
l.5 \setlength{\foo}{2in * \zzz}

如何解決這個問題?

答案1

LaTeX\input命令不可擴展:部分原因是它對檔案是否存在進行「安全性」檢查,部分原因是支援 TeX 原語 ( \input <file>) 和 LaTeX ( \input{<file>}) 語法。這意味著如果您想透過擴充功能來工作,就像在這裡所做的那樣,您需要使用原語

\begin{filecontents*}[overwrite]{size.txt}
5%
\end{filecontents*}
\documentclass{article}
\usepackage{calc}
\newlength{\foo}
\makeatletter
\let\primitiveinput\@@input
\makeatother
\newcommand{\zzz}{\primitiveinput size.txt }
\setlength{\foo}{2in * \zzz}
\usepackage[paperwidth=\foo,paperheight=\foo]{geometry}
\begin{document}
\end{document}

您需要確保您的輸入檔案中沒有令牌\par:我新增了一個使用註解字元的範例。

相關內容