Menu

HowTo_ChapterVerticalLineAfterNumber

How to separate number and text in headings with a vertical line

If you only want a vertical line of fixed height after the chapter number, the simplest solution is to redefine \chapterformat accordingly:

\documentclass{scrreprt}
\renewcommand*{\chapterformat}{%
  \thechapter\autodot\enskip\rule[-\dp\strutbox]{1pt}{\baselineskip}\enskip
}
\usepackage{blindtext}
\begin{document}
\chapter{Lorem ipsum}
\blindtext
\chapter{Lorem ipsum dolor sit amet, consectetur adipisici elit,
  sed eiusmod tempor incidunt}
\blindtext
\end{document}

The result is a vertical line of the same height after the chapter number for both the single-line heading of chapter 1 and the multi-line heading of chapter 2.

Illustration: Table of contents, one page with single-line chapter headings, one page with multi-line chapter headings, each with a short line after the number

However, if you want the depth of the line to extend to the entire height of the multi-line heading in the case of chapter 2, this can no longer be achieved simply by redefining \chapterformat, because within \chapterformat you know neither the text of the heading nor its height and depth or the number of lines required. Instead, it makes sense to redefine the \chapterlinesformat statement, which provides access to both the (already formatted) number and the text:

\documentclass{scrreprt}
\usepackage{tabularx}
\renewcommand*{\chapterformat}{\thechapter\autodot}% horizontal distance after \autodot removed
\renewcommand*{\chapterlinesformat}[3]{% #1 = chapter, #2 = formated number, #3 text
  \IfUseNumber{%
    \setlength{\arrayrulewidth}{1pt}%
    \begin{tabularx}{\linewidth}{@{}l|>{\raggedright}X@{}}
      #2 & #3
    \end{tabularx}%
  }{#3}% no chapter number
}
\usepackage{blindtext}
\begin{document}
\tableofcontents
\chapter{Lorem ipsum}
\blindtext
\chapter{Lorem ipsum dolor sit amet, consectetur adipisici elit,
  sed eiusmod tempor incidunt}
\blindtext
\end{document}

Illustration: Table of contents, one page with single-line chapter headings, one page with multi-line chapter headings, each with a long vertical line in front of the text

The test with \IfUseNumber is used to output only the text of the heading in the case of a heading without a number, for example that of the table of contents. Without the distinction, a line would also appear to the left of the text in this case. The tabularx is the easiest way to create the vertical line and automatically set the column with the text to the maximum width. It should be noted that \IfUseNumber is only valid within headings.

If you want such formatting for all headings, however, it may make sense to also give the left-hand column with the number a fixed width. You can then calculate the width of the right-hand column yourself:

\documentclass{scrreprt}
\usepackage{tabularx}
\renewcommand*{\chapterformat}{\thechapter\autodot}% horizontal distance after \autodot removed
\renewcommand*{\chapterlinesformat}[3]{% #1 = chapter, #2 = formated number, #3 text
  \IfUseNumber{%
    \setlength{\arrayrulewidth}{1pt}%
    \begin{tabularx}{\linewidth}{@{}l|>{\raggedright}X@{}}
      #2 & #3
    \end{tabularx}%
  }{#3}% no chapter number
}
\renewcommand*{\sectionformat}{\thesection\autodot}
\renewcommand*{\subsectionformat}{\thesubsection\autodot}
\renewcommand*{\subsubsectionformat}{\thesubsubsection\autodot}
\setcounter{secnumdepth}{\subsubsectionnumdepth}
\renewcommand*{\sectionlinesformat}[4]{%
  \IfUseNumber{%
    \setlength{\arrayrulewidth}{1pt}%
    \begin{tabular}{@{}>{\raggedleft}p{\numcolwidth}|%
                       >{\raggedright}p{\dimeval{\linewidth-\numcolwidth-2\tabcolsep-\arrayrulewidth}}@{}}
      #3 & #4
    \end{tabular}%
  }{\hskip#2#4}%
}
\newlength{\numcolwidth}\setlength{\numcolwidth}{.5in}% reserved width for the heading numbers
\usepackage{mwe}
\begin{document}
\tableofcontents
\chapter{Lorem ipsum}
\lipsum[1][1-2]
\section{Nam dui ligula}
\lipsum[2][1-2]
\subsection{Nulla malesuada}
\lipsum[3][1-3]
\subsubsection{Quisque ullamcorper}
\lipsum[4]
\chapter{Lorem ipsum dolor sit amet, consectetur adipisici elit,
  sed eiusmod tempor incidunt}
\lipsum[1][1-2]
\section{Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi}
\lipsum[2][1-2]
\subsection{Nulla malesuada porttitor diam. Donec felis erat, congue non,
  volutpat at, tincidunt tristique, libero.}
\lipsum[3][1-3]
\subsubsection{Quisque ullamcorper placerat ipsum. Cras nibh. Morbi vel justo
  vitae lacus tincidunt ultrices.}
\lipsum[4]
\end{document}

Illustration: Table of contents, one page with single-line headings from <code>\chapter</code> to <code>\subsubsection</code>, one page with multi-line headings from <code>\chapter</code> to <code>\subsubsection</code>


Related

Wiki (English): HowTo_Headings

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.