8

I am trying to apply a styling command to every cell of a table's column.

I define a new command invoking minted:

\newcommand{\ccode}[1]{\mintinline{c}{#1}}

And then, I apply this command in the header definition of a table:

\begin{tabular}{| >{\ccode}{l} | l |}

However, this results in some \ignorespaces randomly popping in the output file.

The following is an MWE and the corresponding output.

\documentclass{article}

\usepackage{minted}
\usepackage{collcell}

\newcommand{\ccode}[1]{\mintinline{c}{#1}}

\begin{document}

\begin{tabular}{| >{\ccode}{l} | l |}
    \hline
    int foo (int a, int b) & Foos a and b. \\ \hline
    int bar (int* a, int* b) & Bars a and b. \\ \hline
\end{tabular}

\end{document}

Output file

Has anyone an idea about what is going on? Could it be related to the minted package?

I am running TeX Live 2013 on a Ubuntu 14.04.

Thanks for your help.


Related

How to execute command on every table column

How does the \newcolumntype command work?

1
  • @HeikoOberdiek I've been reading Q/A on this site for months though :) – Right leg Dec 16 '16 at 6:07
10

Usually, LaTeX adds \ignorespaces at the begin of a cell and \unskip at its end to automatically remove spaces at the begin and end of the cell contents.

The example loads package collcell, but does not use it. An example of its usage:

\documentclass{article}

\usepackage{minted}
\usepackage{collcell}

\newcommand{\ccode}[1]{\mintinline{C}{#1}}

\begin{document}

\begin{tabular}{| >{\collectcell{\ccode}}l<{\endcollectcell} | l |}
    \hline
    int foo (int a, int b) & Foos a and b. \\ \hline
    int bar (int* a, int* b) & Bars a and b. \\ \hline
\end{tabular}

\end{document}

Result

PS: I had to use C (uppercase) as language name to get the proper output.

Analysis of the problem in the MWE of the question

First the MWE of the question with some modifications:

  • Uppercase C as language name,
  • Package collcell is not needed, but it loads package array, which implements < and > for the columns specification of the table.

Modified MWE:

\documentclass{article}

\usepackage{array}
\usepackage{minted}

\newcommand{\ccode}[1]{\mintinline{C}{#1}}

\begin{document}

\begin{tabular}{| >{\ccode}l | l |}
    \hline
    int foo (int a, int b) & Foos a and b. \\ \hline
    int bar (int* a, int* b) & Bars a and b. \\ \hline
\end{tabular}

\end{document}

Result of the MWE in the question

\ccode is inserted in front of the cell contents. LaTeX automatically surrounds the cell contents with \ignorespaces and \unskip to remove spaces at the begin and end of a cell. Then the first cell becomes:

 \ccode \ignorespaces int foo (int a, int b)\unskip

Since curly argument braces are missing, the argument of \ccode is the first token \ignorespaces. The remaining part int foo (int a, int )\unskip is set as plain text.

\unskip could be used as end token for the argument and \ignorespaces can be absorbed by adding it to the parameter text of the definition of \ccode:

\def\ccode\ignorespaces#1\unskip{\mintinline{C}{#1}}

Full example:

\documentclass{article}

\usepackage{array}
\usepackage{minted}

\def\ccode\ignorespaces#1\unskip{\mintinline{C}{#1}}

\begin{document}

\begin{tabular}{| >{\ccode}l | l |}
    \hline
    int foo (int a, int b) & Foos a and b. \\ \hline
    int bar (int* a, int* b) & Bars a and b.\\ \hline
\end{tabular}

\end{document}

Package cellcoll hides these internals, providing a more readable and maintainable interface.

3
  • Thank you very much, it perfectly works. I probably was confused by all the syntax of the table-related command. By the way, without the collcell package loaded, my MWE does not compile. – Right leg Dec 16 '16 at 6:28
  • Actually, I still don't understand why my MWE was spawning these \ignorespaces. – Right leg Dec 16 '16 at 6:58
  • 1
    @Rightleg You are grabbing the cell content verbatim, and \ignorespaces is always there as LaTeX itself inserts it at the start of the cell – Joseph Wright Dec 16 '16 at 7:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.