1

With the default definition of \section in the article class, I can make a multiline section header:

\documentclass{article}
\author{}

\begin{document}
\section{test \\ test}
\end{document}

default section article

The following code, allows me to create a "moderncv like section" inside the article class:

\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}

moderncv like section in the article class

However, if I change the \section{test test} to \section{test \\ test} to produce a line break like in the default definition of \section, I receive an error message:

! 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}

As I understood \item, its just being used in lists like itemize or enumerate. Therefore this error message doesn't make sense to me in this context.

  • How can I make this work?
  • And what causes this odd error message?
1
  • 1
    same issue as your previous question, the text is in \raisebox which is like \mbox so can't have \\ (and still missing % in ends of line in the code, as before) Commented May 7, 2016 at 19:06

1 Answer 1

1

Here's one option, by setting the content of the section inside a \parbox which allows for using \\:

enter image description here

\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}

I've added the option of specifying an optional argument like the typical \section for your ToC-related entries.

2
  • Could you please explain what \hintscolumnwidth and \dimexpr does in your code?
    – uuu
    Commented May 7, 2016 at 20:04
  • @toogley: You place the \section text a specific distance away from the left margin. This distance is given by \hintscolumnwidth + \separatorcolumnwidth + 15pt which I remove from \linewidth to set a box that is the width of the line minus all the other elements that is set to the left of the text.
    – Werner
    Commented May 8, 2016 at 6:01

You must log in to answer this question.

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