0

Here's what I'm having now

\documentclass{article}
\usepackage{array}

\newcommand{\test}[1][default]{#1}

\begin{document}
\begin{tabular}{>{\test}c}
[one]1\\
1\\
1\\
1
\end{tabular}

\end{document}

enter image description here

As you can see, \test expands before table's row has been appended. The desired result would be having

\test[one]1\\
\test1\\
\test1\\
\test1

for actual columns, but automated with >{...} column specification. So that the printed result would be

one1
default1
default1
default1

I've also tried to apply \noexpand here, but it doesn't seem to work.

3
  • it isn't expanded first, but the immediatly following tokens are not what you expect, not least, \ignorespaces is inserted Jul 22, 2022 at 10:47
  • 2
    Take a look at the collcell package to get something that might support your use case.
    – Skillmon
    Jul 22, 2022 at 10:48
  • @DavidCarlisle, thank you! I've tried \NewDocumentCommand{\test}{mO{default}}{#2\ignorespaces} and it almost works, but some Missing { rises. How can I fix that.
    – antshar
    Jul 22, 2022 at 10:50

1 Answer 1

2

it isn't expanded first, but the immediately following tokens are not what you expect, not least, \ignorespaces is inserted

\documentclass{article}
\usepackage{array}

%\newcommand{\test}[1][default]{#1}
\newcommand{\test}[2]{\def\qqq{/#1/#2/}\show\qqq}

\begin{document}

\begin{tabular}{>{\test}c}
[one]1\\
1\\
1\\
1
\end{tabular}

\end{document}

shows:

> \qqq=macro:
->/\ignorespaces /[/.
\test #1#2->\def \qqq {/#1/#2/}\show \qqq 
                                          
l.10 [
      one]1\\
? 
> \qqq=macro:
->/\ignorespaces /1/.
\test #1#2->\def \qqq {/#1/#2/}\show \qqq 
                                          
l.11 1
      \\
? 
> \qqq=macro:
->/\ignorespaces /1/.
\test #1#2->\def \qqq {/#1/#2/}\show \qqq 
                                          
l.12 1
      \\
? 

enter image description here

\documentclass{article}
\usepackage{array}

\newcommand{\testb}[1][default]{\ignorespaces#1}
\newcommand\test[1]{\testb}

\begin{document}

\begin{tabular}{>{\test}l}
[one]1\\
1\\
1\\
1
\end{tabular}

\end{document}
2
  • As far as I understand, \def\test\ignorespaces{\testb} is needed only to swallow the first token after \test command and you explicitly set that \ignorespaces is expected right after \test. I tried out to change it to \def\test#1{\testb} and with no surprise it also works. You need two macros \testb and \test only because \necommand doesn't allow to create an optional argument after a mandatory one, but \NewDocumentCommand from xparse can. I wonder, why doesn't \NewDocumentCommand{\test}{ m O{default} }{\ignorespaces#2} work?
    – antshar
    Jul 22, 2022 at 11:31
  • @antshar you need an expandable command to avoid messing up halign processing, you can't use NewExpandableDocumentCommand as it can not do final optional argument. I could use a proper latex #1 for discarding \ignnorespaces as you say, that would be better Jul 22, 2022 at 11:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .