我试图在 生成的表格中使用工程符号(即科学符号,其中十的幂必须是三的倍数)pgfplotstable
。由于pgfplotstable
不支持这种符号,我想使用siunitx
-package 来实现这一点。这是我的示例数据文件“testje.dat”:
-0.974e-4 -7.46e-3
-0.948e-3 -7.45e-3
-0.923e-3 -7.43e-3
-0.845e-3 -7.4e-3
这是我的 LaTeX 代码:
\documentclass{article}
\usepackage{siunitx}
\usepackage{pgfplotstable}
\begin{document}
\pgfkeys{
/pgfplots/table/assign cell content/.code={%
\def\pgfmathresult{#1}%
\ifx\pgfmathresult\pgfutil@empty
\pgfkeyslet{/pgfplots/table/@cell content}\pgfmathresult
\else
\pgfkeyssetvalue{/pgfplots/table/@cell content}{%
\num{scientific-notation = engineering}{#1}%
}%
\fi
},
}
Now I'd like my table-values to be in engineering notation
\begin{table}[h!]
\begin{center}
\pgfplotstabletypeset[
col sep=space,
display columns/0/.style={column name=$U_O$, column type=|c|},
display columns/1/.style={column name=$U_I$, column type=c|,verbatim},
every head row/.style={before row=\hline,after row=\hline},
every last row/.style={after row=\hline},
]{testje.dat}
\end{center}
\caption{Auto generated table from space separated values file
\label{tab:data}}
\end{table}
\end{document}
当我尝试用 LaTeX 编译它时出现以下错误:
siunitx error: "invalid-token-in-number"
Invalid token 's' in numerical input.
See the siunitx documentation for further information.
For immediate help type H <return>.
l.30 ]{testje.dat}
? H
Numbers can only contain tokens defined using the 'input-...' options:
the token 's' is not set up as a valid part of a number.
这里有人能帮我解决这个问题吗?
提前感谢 Hugo
答案1
您无需使用宏来编写自己的代码来排版数字\num
,只需使用S
提供的列类型即可siunitx
(请注意,您还需要使用关键字string type
)。这还允许您使用table-format
对齐选项:
\documentclass[border=5mm]{standalone}
\usepackage{siunitx}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\begin{document}
\pgfplotstabletypeset[
columns/0/.style={
column name=$U_O$,
string type,
column type={S[scientific-notation = engineering, table-format=-3.1e-1]},
},
columns/1/.style={
column name=$U_I$,
string type,
column type={S[scientific-notation = engineering, table-format=-1.2e-1]},
},
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=\bottomrule},
]{
-0.974e-4 -7.46e-3
-0.948e-3 -7.45e-3
-0.923e-3 -7.43e-3
-0.845e-3 -7.4e-3
}
\end{document}
正如 Joseph Wright 在他的评论中指出的那样,将每列中的所有数字除以一个共同的因子可能是一个更好的主意,以使表格更具可读性。您可以使用键即时执行此操作preproc/expr={##1 / 1e-6}
,例如:
\documentclass[border=5mm]{standalone}
\usepackage{siunitx}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\begin{document}
\pgfplotstabletypeset[
multicolumn names,
columns/0/.style={
column name=$U_O / 10^{-6}$,
string type,
column type={S[scientific-notation = engineering, round-mode=figures, round-precision=3, table-format=-3.1]},
preproc/expr={##1/1e-6}
},
columns/1/.style={
column name=$U_I / 10^{-3}$,
string type,
column type={S[scientific-notation = engineering, round-mode=figures, round-precision=3, table-format=-1.2]},
preproc/expr={##1/1e-3}
},
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=\bottomrule},
]{
-0.974e-4 -7.46e-3
-0.948e-3 -7.45e-3
-0.923e-3 -7.43e-3
-0.845e-3 -7.4e-3
}
\end{document}