2

Was playing around with titlesec to design a layout for my master's thesis and have run into a problem where Overleaf/titlesec spits out an error on every section/subsection I insert. It says:

Package titlesec Error: Entered in horizontal mode. The argument cannot contain horizontal material such as text, \noindent, \makebox, etc.

I have used hskip in the titlesec code to shift the section/subsection to the left, which resulted in the error. However, it still works and I get the exact formatting I want. The problem is that I will keep accumulating errors as I add sections, which is not ideal. Does anybody have an idea to circumvent this error while keeping hskip or applying something else that can shift the section without errors?

Here's my code (credit for the chapter heading style goes to Gonzalo Medina from the post linked below. Only adapted it slightly to get what I wanted. How to make this chapter heading style?)

\usepackage{lmodern}
\usepackage{xcolor}
\usepackage{titlesec}
\usepackage{microtype}
% Chapter formatting
\definecolor{new}{RGB}{70,82,155}
\definecolor{myblue}{RGB}{0,82,155}
\titleformat{\chapter}[display]
  {\normalfont\bfseries\color{black}}
  {\filleft\hspace*{-60pt}%
    \rotatebox[origin=c]{90}{%
      \normalfont\color{black}\Large%
        \textls[180]{\textsc{\chaptertitlename}}%
    }\hspace{10pt}%
    {\setlength\fboxsep{0pt}%
    \colorbox{myblue}{\parbox[c][3cm][c]{2.5cm}{%
      \centering\color{white}\fontsize{80}{90}\selectfont\thechapter}%
    }}%
  }
  {10pt}
  {\titlerule\vskip2pt\titlerule[2pt]\vskip2pt\titlerule\vskip8pt\fontsize{40}{50}\vskip-72pt\normalfont}

% Section/Subsection formatting
\titleformat{\section}
   {\normalfont\Large\hskip-45pt}{\colorbox{myblue}{\parbox[c][0.4cm][c]{0.8cm}{\centering\color{white}\fontsize{16}{40pt}\selectfont\thesection}}}{1em}{}
\titleformat{\subsection}
   {\normalfont\large\hskip-45pt}{\colorbox{myblue}{\parbox[c][0.3cm][c]{0.8cm}{\centering\color{white}\fontsize{12}{34pt}\selectfont\thesubsection}}}{1em}{}

Here's an image of my layout for context. Again, looks great! Just want to avoid the hskip error :) enter image description here

1
  • I would guess you want to remove the hskip from the vertical argument and start the horizontal argument with {\hspace*{-45pt}\colorbox.... Feb 18, 2021 at 22:24

1 Answer 1

1

You're using horizontal commands such as \hskip in places where only vertical commands are allowed and conversely.

I reformatted a bit the code and used zero width boxes instead of guessing their widths.

\documentclass{book}

\usepackage{lmodern}
\usepackage{xcolor}
\usepackage{titlesec}
\usepackage{microtype}
\usepackage{rotating}

% Chapter formatting
\definecolor{new}{RGB}{70,82,155}
\definecolor{myblue}{RGB}{0,82,155}

\titleformat{\chapter}[display]
  {\normalfont\bfseries\color{black}}
  {%
   \hfill
   \rotatebox[origin=c]{90}{%
     \normalfont\color{black}\Large\textls[180]{\textsc{\chaptertitlename}}%
   }%
   \hspace{10pt}%
   \begingroup
   \setlength\fboxsep{0pt}%
   \colorbox{myblue}{%
     \parbox[c][3cm][c]{2.5cm}{%
       \centering\color{white}\fontsize{80}{90}\selectfont\thechapter
     }%
   }
   \endgroup
  }
  {10pt}
  {%
   \titlerule
   \vspace{2pt}%
   \titlerule[2pt]%
   \vspace{2pt}%
   \titlerule
   \vspace{8pt}%
   \fontsize{40}{50}\vspace{-72pt}\normalfont
  }

% Section/Subsection formatting
\titleformat{\section}
   {\normalfont\Large}
   {%
    \makebox[0pt][r]{%
      \colorbox{myblue}{%
        \parbox[c][0.4cm][c]{0.8cm}{%
          \centering\color{white}\fontsize{16}{40pt}\selectfont\thesection
        }%
      }%
      \hspace*{10pt}%
    }%
   }
   {0pt}
   {}
\titleformat{\subsection}
   {\normalfont\large}
   {%
    \makebox[0pt][r]{%
      \colorbox{myblue}{%
        \parbox[c][0.3cm][c]{0.8cm}{%
          \centering\color{white}\fontsize{12}{34pt}\selectfont\thesubsection
        }%
      }%
      \hspace*{10pt}%
    }%
   }
   {0pt}
   {}

\begin{document}

\chapter{Test}

\section{Test}

\subsection{Test}

\end{document}

enter image description here

1
  • Thank you egreg. Exactly what I wanted.
    – Sarloos
    Feb 19, 2021 at 12:15

You must log in to answer this question.

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