\section
使用类中的默认定义article
,我可以创建多行节标题:
\documentclass{article}
\author{}
\begin{document}
\section{test \\ test}
\end{document}
这以下代码,允许我在文章类中创建一个“类似 moderncv 的部分”:
\documentclass{article}
\usepackage{fontspec}
\usepackage{xcolor}
\usepackage{xparse}
\author{}
\definecolor{color0}{rgb}{0,0,0}% black
\definecolor{color1}{rgb}{0.22,0.45,0.70}% light blue
\definecolor{color2}{rgb}{0.45,0.45,0.45}% dark grey
\makeatletter
\newlength{\hintscolumnwidth} \setlength{\hintscolumnwidth}{0.175\textwidth}%
\newlength{\baseletterheight} \settoheight{\baseletterheight}{o}%
\newlength{\separatorcolumnwidth} \setlength{\separatorcolumnwidth}{0.025\textwidth}%
% fonts
\newcommand*{\sectionfont}{\Large\mdseries\upshape}%
% styles
\newcommand*{\sectionstyle}[1]{{\sectionfont{#1}}}
\RenewDocumentCommand{\section}{sm}{%
\par\addvspace{2.5ex}%
\addcontentsline{toc}{section}{#2}%
{\strut\raggedleft\raisebox{\baseletterheight}{%
\textcolor{color1}{%
\rule{\hintscolumnwidth}{0.95ex}\hspace*{%
\separatorcolumnwidth}{\strut\sectionstyle{#2}}}}}%
\par\nobreak\addvspace{1ex}\@afterheading%
\makeatother
\begin{document}
\thispagestyle{empty}
\section{test test}
\end{document}
但是,如果我将 更改\section{test test}
为\section{test \\ test}
产生换行符(如 的默认定义中所示)\section
,我会收到一条错误消息:
! LaTeX Error: Something's wrong--perhaps a missing \item.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.33 \section{test \\ test}
据我了解,它只是在或之\item
类的列表中使用。因此,此错误消息在这种情况下对我来说毫无意义。itemize
enumerate
- 我怎样才能让它工作?
- 那么是什么导致了这个奇怪的错误信息呢?
答案1
这是一个选项,通过设置内部部分的内容,\parbox
可以使用\\
:
\documentclass{article}
\usepackage{xcolor}
\usepackage{xparse}
\definecolor{color0}{rgb}{0,0,0}% black
\definecolor{color1}{rgb}{0.22,0.45,0.70}% light blue
\definecolor{color2}{rgb}{0.45,0.45,0.45}% dark grey
\makeatletter
\newlength{\hintscolumnwidth} \setlength{\hintscolumnwidth}{0.175\textwidth}
\newlength{\baseletterheight} \settoheight{\baseletterheight}{o}
\newlength{\separatorcolumnwidth} \setlength{\separatorcolumnwidth}{0.025\textwidth}
% fonts
\newcommand*{\sectionfont}{\Large\mdseries\upshape\raggedright}
% styles
\newcommand*{\sectionstyle}[1]{\parbox[t]{\dimexpr\linewidth-\hintscolumnwidth-\separatorcolumnwidth-15pt}{\sectionfont #1}}
\RenewDocumentCommand{\section}{s o m}{%
\par\addvspace{2.5ex}%
\IfValueTF{#2}
{\addcontentsline{toc}{section}{#2}}%
{\addcontentsline{toc}{section}{#3}}%
{\noindent\strut\raisebox{\baseletterheight}{%
\textcolor{color1}{%
\rule{\hintscolumnwidth}{0.95ex}\hspace*{%
\separatorcolumnwidth}{\strut\sectionstyle{#3}}}}}%
\par\nobreak\addvspace{1ex}\@afterheading}%
\makeatother
\begin{document}
\tableofcontents
\section[test test]{test \\ test}
\end{document}
我添加了指定可选参数的选项,就像\section
ToC 相关条目的典型参数一样。