![如何为多列表中的每一列提供标题](https://linux22.com/image/425949/%E5%A6%82%E4%BD%95%E4%B8%BA%E5%A4%9A%E5%88%97%E8%A1%A8%E4%B8%AD%E7%9A%84%E6%AF%8F%E4%B8%80%E5%88%97%E6%8F%90%E4%BE%9B%E6%A0%87%E9%A2%98.png)
我对 Latex 还很陌生。我想使用 Latex 创建用于培训目的的备忘单。
以下是乳胶代码 -
\documentclass[10pt,english,landscape]{article}
\usepackage{multicol}
\usepackage{calc}
\usepackage[landscape]{geometry}
\usepackage{color,graphicx,overpic}
\usepackage[T1]{fontenc}
\usepackage[bitstream-charter]{mathdesign}
\usepackage[utf8]{inputenc}
\usepackage{url}
\usepackage{amsfonts}
\usepackage{array,booktabs}
\usepackage{textcomp}
\usepackage[usenames,dvipsnames,table]{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{colortbl}
\usepackage{tikz}
\usepackage{environ}
\usetikzlibrary{calc}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\geometry{top=-0.5cm,left=1cm,right=1cm,bottom=1cm}
\pagestyle{empty} % Turn off header and footer
% \renewcommand\rmdefault{phv} % Arial
% \renewcommand\sfdefault{phv} % Arial
% Redefine section commands to use less space
\makeatletter
\renewcommand{\section}{\@startsection{section}{1}{0mm}%
{-1ex plus -.5ex minus -.2ex}%
{0.5ex plus .2ex}%x
{\normalfont\large\bfseries}}
\renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}%
{-1explus -.5ex minus -.2ex}%
{0.5ex plus .2ex}%
{\normalfont\normalsize\bfseries}}
\renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}%
{-1ex plus -.5ex minus -.2ex}%
{1ex plus .2ex}%
{\normalfont\small\bfseries}}
\makeatother
\setcounter{secnumdepth}{0} % Don't print section numbers
\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt plus 0.5ex}
\definecolor{TableHead}{rgb}{0.353, 0.329, 0.667}
\definecolor{TableRow}{rgb}{0.816, 0.812, 0.902}
\NewEnviron{keys}[2][2]{% <-- Modified by default, 2 columns
% \begin{center}
\smallskip
\begin{tikzpicture}
\rowcolors{1}{}{TableRow}
% \centering
\node (tbl) [inner sep=0pt] {
\begin{tabularx}{\linewidth}{*{#1}{X}} % <-- Modified
\rowcolor{TableHead}
\multicolumn{#1}{l}{\normalsize\textbf{\color{white}{#2}}}\parbox{0pt}{\rule{0pt}{0.3ex+\baselineskip}}\\ % <-- Modified
\BODY
\arrayrulecolor{TableHead}\specialrule{.17em}{0em}{.2em}
\end{tabularx}};
\begin{pgfonlayer}{background}
\draw[rounded corners=2pt,top color=TableHead,bottom color=TableHead, draw=white]
($(tbl.north west)-(0,-0.05)$) rectangle ($(tbl.north east)-(0.0,0.15)$);
\draw[rounded corners=2pt,top color=TableHead,bottom color=TableHead, draw=white]
($(tbl.south west)-(0.0,-0.11)$) rectangle ($(tbl.south east)-(-0.0,-0.02)$);
\end{pgfonlayer}
\end{tikzpicture}
% \end{center}
}
\begin{document}
\raggedright\
\begin{center}
\Large{\underline{Emacs Cheatsheet}}
\end{center}
\footnotesize
\begin{multicols}{2}
\begin{keys}[3]{Glossary}
Control (Ctrl) key & \texttt{C} & \texttt{C key for your OS} \\
Alt (Meta) key & \texttt{M} & \texttt{M Key for your OS} \\
Shift key & \texttt{S} & \texttt{M Key for your OS} \\
Command (Super) key & \texttt{CMD} & \texttt{M Key for your OS} \\
Function (Fn) key & \texttt{fn} & \texttt{M Key for your OS} \\
Home/end & \texttt{fn-left / fn-right} & \texttt{M Key for your OS} \\
\end{keys}
\end{multicols}
\end{document}
这将创建如下表 -
如上图所示,其中一列Glossary
运行正常。我想为其他两列提供标题,如词汇表、快捷方式、说明。
我尝试像这样的表头 -
\begin{keys}[3]{Glossary}{Shortcut}{Description}
但它会出现编译错误。
我知道如何修复这个问题吗?
答案1
您只需要向环境添加额外的参数keys
:
\NewEnviron{keys}[4][2]{...}
然后根据需要调用它
\begin{keys}[3]{Glossary}{Shotcut}{Description}
这也消除了使用 的需要\multicolumn
。
代码:
\documentclass[10pt,english,landscape]{article}
\usepackage{multicol}
\usepackage{calc}
\usepackage[landscape]{geometry}
\usepackage{color,graphicx,overpic}
\usepackage[T1]{fontenc}
\usepackage[bitstream-charter]{mathdesign}
\usepackage[utf8]{inputenc}
\usepackage{url}
\usepackage{amsfonts}
\usepackage{array,booktabs}
\usepackage{textcomp}
\usepackage[usenames,dvipsnames,table]{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage{colortbl}
\usepackage{tikz}
\usepackage{environ}
\usetikzlibrary{calc}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}
\geometry{top=-0.5cm,left=1cm,right=1cm,bottom=1cm}
\pagestyle{empty} % Turn off header and footer
% \renewcommand\rmdefault{phv} % Arial
% \renewcommand\sfdefault{phv} % Arial
% Redefine section commands to use less space
\makeatletter
\renewcommand{\section}{\@startsection{section}{1}{0mm}%
{-1ex plus -.5ex minus -.2ex}%
{0.5ex plus .2ex}%x
{\normalfont\large\bfseries}}
\renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}%
{-1explus -.5ex minus -.2ex}%
{0.5ex plus .2ex}%
{\normalfont\normalsize\bfseries}}
\renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}%
{-1ex plus -.5ex minus -.2ex}%
{1ex plus .2ex}%
{\normalfont\small\bfseries}}
\makeatother
\setcounter{secnumdepth}{0} % Don't print section numbers
\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt plus 0.5ex}
\definecolor{TableHead}{rgb}{0.353, 0.329, 0.667}
\definecolor{TableRow}{rgb}{0.816, 0.812, 0.902}
\NewEnviron{keys}[4][2]{% <-- Modified by default, 2 columns
% \begin{center}
\smallskip
\begin{tikzpicture}
\rowcolors{1}{}{TableRow}
% \centering
\node (tbl) [inner sep=0pt] {
\begin{tabularx}{\linewidth}{*{#1}{X}} % <-- Modified
\rowcolor{TableHead}
{\normalsize\textbf{\color{white}{#2}}}\parbox{0pt}{\rule{0pt}{0.3ex+\baselineskip}}
& {\normalsize\textbf{\color{white}{#3}}}\parbox{0pt}{\rule{0pt}{0.3ex+\baselineskip}}
& {\normalsize\textbf{\color{white}{#4}}}\parbox{0pt}{\rule{0pt}{0.3ex+\baselineskip}}
\\
\BODY
\arrayrulecolor{TableHead}\specialrule{.17em}{0em}{.2em}
\end{tabularx}};
\begin{pgfonlayer}{background}
\draw[rounded corners=2pt,top color=TableHead,bottom color=TableHead, draw=white]
($(tbl.north west)-(0,-0.05)$) rectangle ($(tbl.north east)-(0.0,0.15)$);
\draw[rounded corners=2pt,top color=TableHead,bottom color=TableHead, draw=white]
($(tbl.south west)-(0.0,-0.11)$) rectangle ($(tbl.south east)-(-0.0,-0.02)$);
\end{pgfonlayer}
\end{tikzpicture}
% \end{center}
}
\begin{document}
\raggedright\
\begin{center}
\Large{\underline{Emacs Cheatsheet}}
\end{center}
\footnotesize
\begin{multicols}{2}
\begin{keys}[3]{Glossary}{Shotcut}{Description}
Control (Ctrl) key & \texttt{C} & \texttt{C key for your OS} \\
Alt (Meta) key & \texttt{M} & \texttt{M Key for your OS} \\
Shift key & \texttt{S} & \texttt{M Key for your OS} \\
Command (Super) key & \texttt{CMD} & \texttt{M Key for your OS} \\
Function (Fn) key & \texttt{fn} & \texttt{M Key for your OS} \\
Home/end & \texttt{fn-left / fn-right} & \texttt{M Key for your OS} \\
\end{keys}
\end{multicols}
\end{document}