当使用文件输入时,表的第一列怎么会是 siunitx S 列?

当使用文件输入时,表的第一列怎么会是 siunitx S 列?

我的总体目标是创建一个包含所有 S 列的表。在调试时,我找到了这些最​​小示例,现在看来它们几乎太简单了。

按预期工作:

\documentclass{article}
\usepackage{siunitx}

\begin{document}
\begin{tabular}{Sc}
1&2\\
\end{tabular}
\end{document}

失败带有一个相当模糊的错误信息:

\documentclass{article}
\usepackage{siunitx}

\begin{document}
\begin{tabular}{Sc}
\input{\jobname-example_content.tex}
\end{tabular}
\end{document}

错误信息:

! Extra }, or forgotten $.
<recently read> }

l.11 \end
         {tabular}
I've deleted a group-closing symbol because it seems to be
spurious, as in `$x}$'. But perhaps the } is legitimate and
you forgot something else, as in `\hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing `I$}'.

! Extra }, or forgotten $.
<argument> ...{\scan_stop: \c_math_toggle_token }}

l.11 \end
         {tabular}
I've deleted a group-closing symbol because it seems to be
spurious, as in `$x}$'. But perhaps the } is legitimate and
you forgot something else, as in `\hbox{$x}'. In such cases
the way to recover is to insert both the forgotten and the
deleted material, e.g., by typing `I$}'.

! Missing $ inserted.
<inserted text> 
                $
l.11 \end
         {tabular}
I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing `2' now; then
my insertion and my current dilemma will both disappear.

! Missing } inserted.
<inserted text> 
                }
l.11 \end
         {tabular}
I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing `2' now; then
my insertion and my current dilemma will both disappear.

! Missing } inserted.
<inserted text> 
                }
l.11 \end
         {tabular}
I've inserted something that you may have forgotten.
(See the <inserted text> above.)
With luck, this will get me unwedged. But if you
really didn't forget anything, try typing `2' now; then
my insertion and my current dilemma will both disappear.

! Missing control sequence inserted.
<inserted text> 
                \inaccessible 
l.11 \end
         {tabular}
Please don't say `\def cs{...}', say `\def\cs{...}'.
I've inserted an inaccessible control sequence so that your
definition will be completed without mixing me up too badly.
You can recover graciously from this error, if you're
careful; see exercise 27.2 in The TeXbook.

! I can't find file `{test2-example_content.tex}'.
<to be read again> 
                   \color_group_end: 
l.11 \end
         {tabular}
(Press Enter to retry, or Control-D to exit)
Please type another input file name
! Emergency stop.

但是,这些例子都很有效:

\documentclass{article}
\usepackage{siunitx}

\begin{filecontents*}{\jobname-example_content.tex}
1&2\\
\end{filecontents*}

\begin{document}
\begin{tabular}{cS}
\input{\jobname-example_content.tex}
\end{tabular}
\end{document}
\documentclass{article}
\usepackage{siunitx}

\begin{filecontents*}{\jobname-example_content.tex}
1&2\\
\end{filecontents*}

\makeatletter
\newcommand{\expinput}[1]{\@@input #1}
\makeatother

\begin{document}
\begin{tabular}{Sc}
\expinput{\jobname-example_content.tex}
\end{tabular}
\end{document}

总之,仅当我从文件输入值并且第一列是 S 列时才会出现问题。

这是我的版本信息:

$ pdflatex -version
pdfTeX 3.14159265-2.6-1.40.20 (TeX Live 2019)

相关问题:

  • 解决方法\@@input源于我之前的一个回答问题
  • 类似的问题是在这里长大。然而,那里交织着许多不同的方面,而唯一的答案包含一个不使用的版本\input

答案1

只需更改\input{\jobname-example_content.tex}\protect\input{\jobname-example_content.tex},或者更好的是 \protect\input\jobname-example_content

这是必要的,因为S默认情况下,列类型会以各种复杂的方式解析其输入,从而使其参数变得“脆弱”(在 LaTeX 特定意义上)。当然,如果您执行,则不需要\sisetup{parse-numbers=false}该指令。\protect

\documentclass{article}
\begin{filecontents*}[overwrite]{\jobname-example_content.tex}
1&2\\
\end{filecontents*}
\usepackage{siunitx}
\begin{document}

% Either one of the two following methods "works":

\begin{tabular}{Sl}
\protect\input\jobname-example_content
\end{tabular}

\sisetup{parse-numbers=false}
\begin{tabular}{Sc}
\input\jobname-example_content % observe: no \protect instruction
\end{tabular}

\end{document}

相关内容