4

I try to define macro for shortcut form \multiline{1}{<pcolumn type>}{\textbf{text}}:

\documentclass[12pt,border=1mm,preview]{standalone}

\newcommand\mcbf[2]{\multicolumn{1}{#1}{\textbf{#2}}}
    \begin{document}
\begin{tabular}{lr}
    \hline
\mcbf{|c|}{AAA}  &   \mcbf{c|}{BBB}   \\ \hline
\end{tabular}
    \end{document}

Which work as expected. Since in the most a case I use the same column specifier i try to redefine this macro so, that the default column type is for example c|:

\documentclass[12pt,border=1mm,preview]{standalone}

\newcommand\xmcbf[2][c|]{\multicolumn{1}{#1}{\textbf{#2}}}

    \begin{document}
\begin{tabular}{lr}
\xmcbf[|c|]{AAA} &   \mcbf{BBB}  \\ \hline
C                &   D           \\ \hline
\end{tabular}
    \end{document}

Which gives error:

\multispan ->\omit
                  \@multispan

What I miss in \newcommand definition? My (probably clumsy) search in SE doesn't gives useful information.

1
  • I assume \mcbf is a typo for \xmcbf Commented Jul 30, 2015 at 14:15

1 Answer 1

7

You need the command to expand to \multicolumn

\documentclass[12pt,border=1mm,preview]{standalone}
\usepackage{xparse}

\DeclareExpandableDocumentCommand\xmcbf{O{c|}m}{\multicolumn{1}{#1}{\textbf{#2}}}

    \begin{document}
\begin{tabular}{lr}
\xmcbf[|c|]{AAA} &   \xmcbf{BBB}  \\ \hline
C                &   D           \\ \hline
\end{tabular}
    \end{document}
5
  • @DavidCalise, thank you. This is over my knowledge of LaTeX ... but now I have solution :-)
    – Zarko
    Commented Jul 30, 2015 at 14:25
  • @DavidCalise,usually, when I stuck in LaTeX I look in LaTeX Companion (I owned second, printed edition). It is inestimable source, when I need insight to some rare used package or looking for solutions which provides packages for which I'm not aware. I wonder, if we can expected its third edition which will incorporate between other xparse package?
    – Zarko
    Commented Jul 30, 2015 at 14:52
  • @Zarko perhaps, one day.... Commented Jul 30, 2015 at 16:00
  • @Zarko IMHO, TeXbook is an "inestimable source". The answer to your problem can be found here: \omit or \span must be first primitive command(s) after full expansion and space ignoring in data area in \halign. It means that your problem isn't typically LaTeX problem but it is TeX problem. And I doubt that such answer can be found in LaTeX documentation.
    – wipet
    Commented Jul 31, 2015 at 6:30
  • 1
    @wipet but that answer doesn't really help as knowing that doesn't tell you how to define parsing for an optional [] argument in an expandable way, which is the point of the question, and the point of \DeclareExpandableDocumentCommand Commented Jul 31, 2015 at 8:25

You must log in to answer this question.

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