5

I want to apply \lstinline to one of the columns. I don't like idea to put it in every cell manually.

I am trying to do

\begin{table}
    \begin{center}
        \begin{tabular}{| >{\lstinline}l | l |}
        \hline
Имя класса & Назначение\\ \hline
Base & Базовый класс с функциями-утилитами \\ \hline
        \end{tabular}
    \end{center}
\end{table}

But pdflatex just hangs on these lines.

6
  • What is it that you want to do, since there might be other ways of accomplishing what you're after.
    – Werner
    Jun 14, 2012 at 3:09
  • @Werner I want words in one column to be styled as listing. I can write \lstinline$class_name$ in every column (at least I can use regular expressions to do that automatically) but I hope there are easier ways to do this.
    – efpies
    Jun 14, 2012 at 3:13
  • Is there a reason why this needs to be inside a tabular environment?
    – Werner
    Jun 14, 2012 at 3:41
  • @Werner Yes. This should be a placed in a table.
    – efpies
    Jun 14, 2012 at 4:11
  • Did you figure out a way to solve this problem? I would like to do exactly the same thing...
    – Robert
    Jun 3, 2016 at 9:07

1 Answer 1

3

You may try the collcell package, which allows you to define a column type that applies a macro to the content of each cell of columns of that type. Its applicability might depend on what exactly you want to achieve with \lstinline -- refer to the manual e.g. for the verb option.

\documentclass{article}

\usepackage{array}
\usepackage{collcell}
\usepackage{listings}

\newcommand{\mymacroA}[1]{\textbf{#1}}
\newcommand{\mymacroB}[1]{\lstinline{#1}}

\newcolumntype{E}{>{\collectcell\mymacroA}l<{\endcollectcell}}
\newcolumntype{F}{>{\collectcell\mymacroB}l<{\endcollectcell}}  

\begin{document}
\begin{table}
    \begin{center}
        \begin{tabular}{| E | F |}
        \hline
        Text & Text \\\hline
        Text & Text \\\hline
        \end{tabular}
    \end{center}
\end{table}

\end{document}
1
  • Works like a charm, however, how about adding column headers? \textbf{Column 1} & \textbf{Column 2} \\ soon after the \begin{tabular}{|E|F|}, for instance, generates an error... Jun 30, 2019 at 12:56

You must log in to answer this question.

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