4

I have the following command defined for a custom row of a two-column tabularx table with column specifiers rX:

\newcommand{\pbrow}[2]{\pbox[t]{\linewidth}{#1} & \pbox[t]{\linewidth}{#2} \\}

The content of the left column will always be formatted using a typewriter font. Now, I need two different typewriter fonts in my document, the first of which, I've set as my default teletype font. For the second one, I've defined the following command:

\newcommand{\nkeyw}[1]{\begingroup \fontfamily{qcr}\selectfont #1\endgroup}

In the above table, I need the use of this 2nd typewriter font. Since the content of cells are typeset inside a pbox, I explicitly need to demarcate the end of line using a \\. Therefore, my usage of the pbrow command is something like this:

\pbrow{\nkeyw{line 1} \\ \nkeyw{line 2} \\ .... \nkeyw{line n}}{Not concerned with 2nd arg}

Now this is a bit tiresome, so instead I thought of redefining pbrow to format the entire argument with nkeyw as follows:

\newcommand{\pbrow}[2]{\pbox[t]{\linewidth}{\nkeyw{#1}} & \pbox[t]{\linewidth}{#2} \\}

Unfortunately, I'm getting an error because of the \\s

7
  • Do you know what the delimiters look like? Or could they be arbitrary?
    – Werner
    Jun 29, 2013 at 16:30
  • It could be anything!
    – Chatterjee
    Jun 29, 2013 at 16:30
  • 2
    Could you provide some examples please? Be exhaustive.
    – Werner
    Jun 29, 2013 at 16:33
  • Added the situation!
    – Chatterjee
    Jun 29, 2013 at 16:55
  • How are you switching to the "normal teletype" font, using \texttt{..}? How about a switch rather: \ttfamily.
    – Werner
    Jun 29, 2013 at 17:01

1 Answer 1

5

I would define a font switch command that can be used inside a cell or \pbox. For example:

\documentclass{article}
\usepackage{tabularx}
\usepackage{pbox}

\newcommand*{\nkeywfont}{\fontfamily{pcr}\selectfont}
\newcommand{\nkeyw}[1]{\begingroup\nkeywfont#1\endgroup}

\newcommand*{\pbrow}[2]{%
  \pbox[t]{\linewidth}{\nkeywfont #1\unskip\strut} &
  \pbox[t]{\linewidth}{#2} \\%
}

\begin{document}
\noindent
\begin{tabularx}{\linewidth}{rX}
  \pbrow{Line 1\\Line 2}{2nd argument}
  \pbrow{foo}{bar}
\end{tabularx}
\end{document}

Result

Remark:

  • I have added \strut at the last line of \pbox to improve the line spacing to the next table row.

Alternative

If explicit line breaks are always used, then a tabular can be used instead of \pbox:

\documentclass{article}
\usepackage{tabularx}
\usepackage{pbox}

\newcommand*{\nkeywfont}{\fontfamily{pcr}\selectfont}
\newcommand{\nkeyw}[1]{\begingroup\nkeywfont#1\endgroup}

\newcommand*{\pbrow}[2]{%
  \nkeywfont
  \begin{tabular}[t]{@{}l@{}}#1\end{tabular} &
  \pbox[t]{\linewidth}{#2} \\%
}

\begin{document}
\noindent
\begin{tabularx}{\linewidth}{rX}
  \pbrow{Line 1\\Line 2}{2nd argument}
  \pbrow{foo}{bar}
\end{tabularx}
\end{document}

Result

Package array

It is also possible to add a command to a cell automatically. It can be overwritten by \multicolumn{1}{...}{...}.

\documentclass{article}
\usepackage{tabularx}
\usepackage{pbox}

\newcommand*{\nkeywfont}{\fontfamily{pcr}\selectfont}
\newcommand{\nkeyw}[1]{\begingroup\nkeywfont#1\endgroup}

\newcommand*{\pbrow}[2]{%
  \begin{tabular}[t]{@{}l@{}}#1\end{tabular} &
  \pbox[t]{\linewidth}{#2} \\%
}

\begin{document}
\noindent
\begin{tabularx}{\linewidth}{>{\nkeywfont}rX}
  \pbrow{Line 1\\Line 2}{2nd argument}
  \pbrow{foo}{bar}
\end{tabularx}
\end{document}

Result

4
  • Your first solution is logically the same as mine. You're separating the \fontfamily{pcr}\selectfont into a separate command, and the newlines are working fine. Is it because of the \begingroup \endgroup that errors are being produced for passing newlines to \nkeyw?
    – Chatterjee
    Jun 29, 2013 at 20:20
  • 2
    @Chatterjee: \pbox is more or less a wrapper around tabular. Thus its limitations apply, groups are confined to a cell and groups that crosses cells or rows cannot be used. Jun 29, 2013 at 20:28
  • Should I change the title of the question then to reflect my specific situation?
    – Chatterjee
    Jun 29, 2013 at 20:30
  • @Chatterjee: Do what you think is best, I do not have a better suggestion. Jun 29, 2013 at 20:33

You must log in to answer this question.

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