如何自动为表格的一列使用打字机字体?

如何自动为表格的一列使用打字机字体?

我正在尝试创建一个表格,其中一列中的所有内容都采用打字机字体。换句话说,它应该像这样执行:

\begin{tabular} {| l | p{5cm} |}
\hline
\multicolumn{2}{|l|}{Sample Table} \\ \hline
\texttt{left a} & right a \\ \hline
\texttt{left b} & right b \\ \hline
\texttt{left c} & right c \\ \hline
\end{tabular}

但是,我想避免\texttt在每一行都输入内容。有办法吗?

答案1

您可以使用array包及其高级列规范。语法是

>{before-code} column-type <{after-code}

其中before-codeafter-code分别在相关列的每个单元格的开头和结尾执行。在这里,你应该使用

>{\ttfamily}l

这将产生一个左对齐的列,其单元格以打字机字体排版。

在此处输入图片描述

\documentclass{article}

\usepackage{array}

\begin{document}

\begin{tabular} {| >{\ttfamily}l | p{5cm} |}
\hline
\multicolumn{2}{|l|}{Sample Table} \\ \hline
left a & right a \\ \hline
left b & right b \\ \hline
left c & right c \\ \hline
\end{tabular}

\end{document}

相关内容