![设置表格中单词的代码字体样式](https://linux22.com/image/471217/%E8%AE%BE%E7%BD%AE%E8%A1%A8%E6%A0%BC%E4%B8%AD%E5%8D%95%E8%AF%8D%E7%9A%84%E4%BB%A3%E7%A0%81%E5%AD%97%E4%BD%93%E6%A0%B7%E5%BC%8F.png)
最有价值球员:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{lmodern}
\usepackage{color}
\usepackage{tcolorbox}
\usepackage{float} % Better figure and table placements
\usepackage{tabularx}
\usepackage{tabularray}
\usepackage{multirow}
\begin{document}
\providecolor{keywords}{HTML}{aa5099}
{\renewcommand{\arraystretch}{1.3}%
\begin{table}[H]
\centering\sffamily\tabcolsep=2.5ex\relax
\footnotesize
\begin{tabular}{>{\color{keywords}}l>{\color{keywords}}l>{\color{keywords}}l>{\color{keywords}}l>{\color{keywords}}l}
cond & in & not & boolean & break\\
const & continue & decimal & default & define \\
do & intersect & union & loop & infinity \\
lambda & sum & division & pi & tau \\
sin & cos & tan & int & float \\
rational & real & complex&&
\end{tabular}\par
\end{table}
}
\end{document}
输出:
我想要一个像字体一样的代码,就像使用\texttt{}
或时一样\verb
。如何在一个地方设置它并使用相同的字体影响所有值?
答案1
如果表格内的所有内容都应该以相同的方式处理,则设置字体(和颜色)更改外部这tabular
:
\documentclass{article}
\usepackage{xcolor}
\begin{document}
\providecolor{keywords}{HTML}{aa5099}
\begin{center}
\setlength{\tabcolsep}{2.5ex}%
\renewcommand{\arraystretch}{1.3}%
\footnotesize\ttfamily
\color{keywords}%
\begin{tabular}{ *{5}{l} }
cond & in & not & boolean & break \\
const & continue & decimal & default & define \\
do & intersect & union & loop & infinity \\
lambda & sum & division & pi & tau \\
sin & cos & tan & int & float \\
rational & real & complex & &
\end{tabular}
\end{center}
\end{document}
您不必tabular
在里面设置table
。它也可以独立存在。
答案2
使用tabularray
包装很简单:
\documentclass{article}
\usepackage{lmodern}
\usepackage{xcolor}
\providecolor{keywords}{HTML}{aa5099}
\usepackage{tabularray}
\begin{document}
\begin{table}[!ht]
\begin{tblr}{colspec={@{} *{5}{X[l, font=\ttfamily, fg=keywords]} @{}}}
cond & in & not & boolean & break \\
const & continue & decimal & default & define \\
do & intersect & union & loop & infinity \\
lambda & sum & division & pi & tau \\
sin & cos & tan & int & float \\
rational & real & complex & &
\end{tblr}
\end{table}
\end{document}