# 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?

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.

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

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

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

-
But I noticed that \verb|\test| is not exactly the same as \texttt{\textbackslash test}. – kiss my armpit 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

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

• 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)

• 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.

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


# \detokenize

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

• 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

-
Please reduce the number of keystrokes and apply the DRY (Don't Repeat Yourself) principle. :-) – kiss my armpit 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}
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},