6

How do I reuse cell delimiter of a tabular environment (usually ampersand) for delimiting a macro parameter?

Like this (which unfortunately doesn't work):

\def\mymacro#1&{#1}

...

\begin{tabular}
Hello & \mymacro out & there \\
\end{tabular}

where \mymacro shall only apply to out.

I'd rather not use curly braces in this particular case to delimit the parameter value.

Also, I should mention that a space won't work either as there might be spaces in the parameter value as well.

ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149

1 Answers1

5

The exact rules of how macro expansion interacts with table scanning are somewhat arcane but in this instance you can do this:

enter image description here

\documentclass{article}


\begin{document}

or a version that works even if not the first token in the cell.

enter image description here

\def\mymacro#1&{\fbox{#1\unskip}&}


\documentclass{article}


\begin{document}

\def\mymacro{\relax\mymacrox}
\def\mymacrox#1\unskip{\fbox{#1\unskip}}

\begin{tabular}{ccc}
Hello &aaa \mymacro out & there \\
Hello &\mymacro out & there \\
\end{tabular}

\end{document}

\begin{tabular}{ccc}
Hello & \mymacro out & there \\
\end{tabular}

\end{document}
David Carlisle
  • 757,742