fancyvrb

fancyvrb

我研究过手册的源代码siunitx。对于新手来说,它太复杂了。我的目标是避免重复,ampere例如,在下表的每一列中。我只想在或主体中ampere为第一行、candela第二行等输入一个,然后我就可以得到表格了。tabularlongtable

在此处输入图片描述

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs,longtable,array}
\usepackage[active,tightpage]{preview}
\PreviewBorder=0pt

\ifPreview \let\longtable\tabular \let\endlongtable\endtabular \fi 
\PreviewEnvironment{longtable}

\begin{document}
\begin{longtable}{*3{m{\dimexpr0.5\linewidth/3\relax}}}
        \toprule
        Unit & Macro & Symbol\\
        \midrule
        ampere & \verb|\ampere| & \si{\ampere}\\
        candela & \verb|\candela|& \si{\candela}\\
        kelvin & \verb|\kelvin|& \si{\kelvin}\\
        kilogram & \verb|\kilogram|& \si{\kilogram}\\
        meter & \verb|\meter|& \si{\meter}\\
        mole & \verb|\mole|& \si{\mole}\\
        second & \verb|\second|& \si{\second}\\
        \bottomrule
\end{longtable}
\end{document}

如何利用单元格的前一个普通值作为\verb下一列的参数和宏名称?

答案1

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs,longtable,array}
\usepackage[active,tightpage]{preview}
\PreviewBorder=0pt

\ifPreview \let\longtable\tabular \let\endlongtable\endtabular \fi 
\PreviewEnvironment{longtable}

 \def\z#1{#1&\texttt{\expandafter\string\csname#1\endcsname}&\expandafter\si\csname#1\endcsname\\}

\begin{document}
\begin{longtable}{*3{m{\dimexpr0.5\linewidth/3\relax}}}
        \toprule
        Unit & Macro & Symbol\\
        \midrule
        \z{ampere}
        \z{candela}
        \z{kelvin}
        \z{kilogram}
        \z{meter}
        \z{mole}
        \z{second}
        \bottomrule
\end{longtable}
\end{document}

答案2

fancyvrb

优点

  • 绝对真实逐字逐句。
  • 节省了打字的时间\verb

缺点

  • 不是全自动的。

代码

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs,longtable,fancyvrb}

\usepackage[active,tightpage]{preview}
\PreviewBorder=0pt
\ifPreview \let\longtable\tabular \let\endlongtable\endtabular \fi 
\PreviewEnvironment{longtable}

\DefineShortVerb{\|}
\begin{document}
\begin{longtable}{lls}
    \toprule
    Unit     & Macro       & Symbol    \\ \midrule
    ampere   & |\ampere|   & \ampere   \\
    candela  & |\candela|  & \candela  \\
    kelvin   & |\kelvin|   & \kelvin   \\
    kilogram & |\kilogram| & \kilogram \\
    meter    & |\meter|    & \meter    \\
    mole     & |\mole|     & \mole     \\
    second   & |\second|   & \second   \\ \bottomrule
\end{longtable}
\end{document}

输出

(参见下一个输出。)

collcell&\string (+s列)

优点

  • 你可以\<macro>毫无问题地书写。
  • 不需要T1/ lmodern(但这也不会有害)。
  • 您可以使用 隐藏第一行\multicolumn

缺点

  • 并非真正的逐字逐句。

代码

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs,longtable,collcell}

\newcommand*{\myString}[1]{\texttt{\string#1}}
\newcolumntype{v}{>{\collectcell\myString}l<{\endcollectcell}}

\usepackage[active,tightpage]{preview}
\PreviewBorder=0pt
\ifPreview \let\longtable\tabular \let\endlongtable\endtabular \fi 
\PreviewEnvironment{longtable}

\begin{document}
\begin{longtable}{lvs}
    \toprule
    Unit     & \multicolumn{1}{l}{Macro} & Symbol    \\ \midrule
    ampere   & \ampere                   & \ampere   \\
    candela  & \candela                  & \candela  \\
    kelvin   & \kelvin                   & \kelvin   \\
    kilogram & \kilogram                 & \kilogram \\
    meter    & \meter                    & \meter    \\
    mole     & \mole                     & \mole     \\
    second   & \second                   & \second   \\ \bottomrule
\end{longtable}
\end{document}

输出

在此处输入图片描述

\detokenize

优势

  • 你可以\<macro>毫无问题地书写。
  • 几乎完全逐字逐句
    • 允许多个宏
    • \\%并且&不允许,括号必须匹配

缺点

  • 第一行被去标记化了。
  • 您不能在第一行使用任何宏。

代码

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs,longtable,collcell}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\newcounter{rowcounter}
\newcolumntype{v}{>{\ifnum\value{rowcounter}>0\relax\ttfamily\fi\collectcell\detokenize}l<{\endcollectcell\stepcounter{rowcounter}}}

\usepackage[active,tightpage]{preview}
\PreviewBorder=0pt
\ifPreview \let\longtable\tabular \let\endlongtable\endtabular \fi 
\PreviewEnvironment{longtable}

\expandafter\def\expandafter\tabular\expandafter{\tabular\setcounter{rowcounter}{0}}
\begin{document}
\begin{longtable}{lvl}
    \toprule
    Unit     & Macro     & Symbol         \\ \midrule
    ampere   & \ampere   & \si{\ampere}   \\
    candela  & \candela  & \si{\candela}  \\
    kelvin   & \kelvin   & \si{\kelvin}   \\
    kilogram & \kilogram & \si{\kilogram} \\
    meter    & \meter    & \si{\meter}    \\
    mole     & \mole     & \si{\mole}     \\
    second   & \second   & \si{\second}   \\ \bottomrule
\end{longtable}
\end{document}

输出

在此处输入图片描述

答案3

这基本上就是大卫的回答所做的,但可能更方便一些,因为您只需将您的单位存储在类似数组的结构中,其余部分则是即时创建的。

\documentclass{article}
\usepackage{pgfplotstable,siunitx,booktabs}
\pgfplotstableread{
Unit
ampere
candela
kelvin 
kilogram 
meter
mole
second
}\myunitlist

\begin{document}
\pgfplotstableset{
create on use/Macro/.style={create col/copy={Unit}},
create on use/Symbol/.style={create col/copy={Unit}}
}

\pgfplotstabletypeset[string type,
column type=l,
columns={Unit,Macro,Symbol},
columns/Macro/.style={postproc cell content/.style={@cell content/.add={\ttfamily\expandafter\string\csname}{\endcsname}}},
columns/Symbol/.style={postproc cell content/.style={@cell content/.add={\expandafter\si\csname}{\endcsname}}},
every head row/.style={before row=\toprule,after row=\midrule},
every last row/.style={after row=\bottomrule},
]{\myunitlist}

\end{document}

在此处输入图片描述

相关内容