[Tex/LaTex] Fancy section headings

formattingsectioningsections-paragraphs

Would someone construct the following style of section heading (intended for longer reports and books)? (The dashed line is meant to be solid.)

      |
1.1   |   SECTION HEADING (IT WOULD EXTEND AT MOST TO ABOUT
      |   HERE, I.E., APPROX. 2/3 OF THE PAGE)
      |

The following parameters should be adjustable, so that people can fit the template to their own needs:

  • Thickness of the line
  • Length of the line above and below the section heading text
  • Space above and below the section heading block
  • Space to the left and right of the line
  • Length of the text box before it wraps around.

Edit. There is a small problem: the vertical line doesn't extend the same amount above and below the text (when it is only one line), as shown below.

Also: this wasn't included in the original question, but it would be nice to have the option of using separate fonts for the numbers and section heading (so that, for example, the section number could be bold but not the section heading, as in the chapter heading I'm using).

Thanks for the solution, it looks great otherwise.

enter image description here

Best Answer

Improved version

Here is a possibility using the xkeyval package and a key-value approach:

enter image description here

The code (explanations below):

\documentclass{article}
\usepackage[explicit]{titlesec}
\usepackage{xkeyval}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}
\usepackage{lipsum}

\newlength\LeftSep
\newlength\RightSep
\newlength\TitleWd
\newlength\VertLineWd
\newlength\SpaceBefore
\newlength\SpaceAfter
\newlength\RuleAddition
\newcommand\SectionFont{\normalfont\Large\bfseries}
\newsavebox\TitleBox
\newcounter{tmp}

\setlength\LeftSep{\marginparsep}
\setlength\RightSep{\marginparsep}
\setlength\TitleWd{0.6666\textwidth}
\setlength\VertLineWd{1pt}
\setlength\SpaceBefore{3.5ex plus 1ex minus .2ex}
\setlength\SpaceAfter{2.3ex plus .2ex}
\setlength\RuleAddition{0pt}

\makeatletter
\define@key{fctaylor}{leftsep}{\setlength\LeftSep{#1}}
\define@key{fctaylor}{rightsep}{\setlength\RightSep{#1}}
\define@key{fctaylor}{titlewidth}{\setlength\TitleWd{#1}}
\define@key{fctaylor}{verticalrulewidth}{\setlength\VertLineWd{#1}}
\define@key{fctaylor}{spacebefore}{\setlength\SpaceBefore{#1}}
\define@key{fctaylor}{spaceafter}{\setlength\SpaceAfter{#1}}
\define@key{fctaylor}{sectionfont}{\renewcommand\SectionFont{#1}}
\define@key{fctaylor}{ruleaddition}{\setlength\RuleAddition{#1}}

\newcommand\FCsectionformat[1][]{%
\setkeys{fctaylor}{#1}
\titleformat{\section}
  {\SectionFont}{}{0em}
  {%
    \parbox[t]{1em}{\thesection}\hspace{\LeftSep}%
    \stepcounter{tmp}%
    \tikz[remember picture]
      \draw[overlay,line width=\VertLineWd] 
        ([xshift=-\RightSep,yshift=\dimexpr\ht\strutbox+\RuleAddition\relax]pic cs:start-\thetmp) -- 
        ( $ ({pic cs:start-\thetmp}|-{pic cs:end-\thetmp}) + (-\RightSep,\dimexpr+\ht\strutbox-\baselineskip-\RuleAddition\relax)$ );%
    \hspace{\RightSep}% 
    \parbox[t]{\TitleWd}{%
      \SectionFont\raggedright\strut%
      \tikzmark{start-\thetmp}##1\tikzmark{end-\thetmp}\strut}%
  }
\titleformat{name=\section,numberless}
  {\SectionFont}{}{0em}
  {%
   \hspace*{\dimexpr1em+\LeftSep\relax}%
    \stepcounter{tmp}%
    \tikz[remember picture]
      \draw[overlay,line width=\VertLineWd] 
        ([xshift=-\RightSep,yshift=\dimexpr\ht\strutbox+\RuleAddition\relax]pic cs:start-\thetmp) -- 
        ( $ ({pic cs:start-\thetmp}|-{pic cs:end-\thetmp}) + (-\RightSep,\dimexpr+\ht\strutbox-\baselineskip-\RuleAddition\relax)$ );%
    \hspace{\RightSep}% 
    \parbox[t]{\TitleWd}{%
      \SectionFont\raggedright\strut%
      \tikzmark{start-\thetmp}##1\tikzmark{end-\thetmp}\strut}%
   }
}
\titlespacing*{\section}
  {0pt}{\SpaceBefore}{\SpaceAfter}
\titlespacing*{name=\section,numberless}
  {0pt}{\SpaceBefore}{\SpaceAfter}
\makeatother

\FCsectionformat

\begin{document}

\section{This is a test numbered section with a long title}
\lipsum[4]
\section*{This is a test unnumbered section with a long title}
\lipsum[4]

\section{This is a test numbered section}
\lipsum[4]
\section*{This is a test unnumbered section}
\lipsum[4]

\FCsectionformat[
  leftsep=30pt,
  rightsep=20pt,
  verticalrulewidth=4pt,
  titlewidth=\dimexpr\textwidth-2.5em-\RightSep-\LeftSep-\VertLineWd\relax,
  spacebefore=1cm,
  spaceafter=0.5cm,
  sectionfont=\normalfont\LARGE\itshape,
  ruleaddition=10pt
]
\section{This is a test numbered section with a long title}
\lipsum[4]
\section*{This is a test unnumbered section with a long title}
\lipsum[4]

\section{This is a test numbered section}
\lipsum[4]
\section*{This is a test unnumbered section}
\lipsum[4]

\end{document}

Explanation:

The main command is \FCsectionformat which can be used anywhere in the document (in the preamble, or in the body of the document), as many times as desired; the command has an optional argument to change the title attributes by using a comma separated list of <key>=<value> options.

The available keys are:

  • leftsep=<length>, the separation between the number and the vertical rule; default: \marginparsep.

  • rightsep=<length>, the separation between the vertical rule and the title; default: \marginparsep.

  • verticalrulewidth=<length>, the thickness of the vertical rule; default: 1pt.

  • titlewidth=<length>, the width of the box to typeset the title; default: 0.6666\textwidth.

  • spacebefore=<length>, space before the title; default: 3.5ex plus 1ex minus .2ex.

  • spaceafter==<length>, space after the title; default: 2.3ex plus .2ex.

  • sectionfont=, the font specification for the title; default: \normalfont\LARGE\itshape.

  • ruleaddition=<length>, the additional length to be added above and below to the vertical rule; default: 0pt.