Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have explored the source codes of siunitx manual. It is too complicated for a newbie. My objective is to avoid repeating, ampere for example, in each column of the following table. I just want to type a single ampere for the first row, candela in the second row, etc in the tabular or longtable body and I get the table.

enter image description here

\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}

How to make use of the previous plain value of a cell as an argument for \verb and a macro name in the next columns?

share|improve this question
1  
Solution 1: Use \DefineShortVerb from the fancyvrb package (saves typing). Solution 2: Use an editor that 2a) aligns tabular material at the & and 2b) provides block editing. Solution 3. Combine David Carlisle’s answer with the collcell package. –  Qrrbrbirlbel Jan 6 '13 at 2:52
1  
Comment on your implicit criticism of David Carlisle's solution as not simple: excluding longtable stuff the content is simply that \csname#1\endcsname turns a name into a macro, and \expandafter\string\csname#1\endcsname turns it into a textual representation of the control sequence. With \csname and \string being defined precisely for these purposes, that's as simple as it gets. Note that you don't really need \verb to format the macro, since its two functions, to prevent expansion and change the font, are separately taken care of by \string and \texttt. –  Ryan Reich Jan 6 '13 at 3:01

3 Answers 3

up vote 8 down vote accepted
+50
\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}
share|improve this answer
    
But I noticed that \verb|\test| is not exactly the same as \texttt{\textbackslash test}. –  cyanide-based food Aug 7 '12 at 19:34
    
Sorry it would work in T1 encoding but not in the default OT1, I changed it to use \string –  David Carlisle Aug 7 '12 at 19:38

fancyvrb

Advantages

  • Absolute real verbatim.
  • Saves the typing of \verb.

Disadvantages

  • Not fully automatic.

Code

\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}

Output

(See next output.)

collcell & \string (+ s column)

Advantages

  • You can write \<macro> without a problem.
  • Does not need T1/lmodern (but that would not hurt either).
  • You can hide the first line with \multicolumn.

Disadvantages

  • Not real verbatim.

Code

\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}

Output

enter image description here

\detokenize

Advantage

  • You can write \<macro> without a problem.
  • Nearly full verbatim
    • More than one macro allowed
    • \\, % and & are not allowed, braces must match

Disadvantages

  • The first line gets de-tokenized.
  • You cannot use any macros in the first line.

Code

\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}

Output

enter image description here

share|improve this answer
    
Please reduce the number of keystrokes and apply the DRY (Don't Repeat Yourself) principle. :-) –  cyanide-based food Jan 6 '13 at 3:03

This is essentially what David's answer does but maybe somewhat more convenient as you just store your units in an array-like structure and the rest is created on the fly.

\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}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.