# Defining multicolum by macro with optional argument

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.

• I assume \mcbf is a typo for \xmcbf – David Carlisle Jul 30 '15 at 14:15

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}

• @DavidCalise, thank you. This is over my knowledge of LaTeX ... but now I have solution :-) – Zarko Jul 30 '15 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 Jul 30 '15 at 14:52
• @Zarko perhaps, one day.... – David Carlisle Jul 30 '15 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 Jul 31 '15 at 6:30
• @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 – David Carlisle Jul 31 '15 at 8:25