2

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

3
  • Should we interpret your drawing as the vertical line lies on the left margin? Which lines above and below the title (your "drawing" doesn't show any lines above and below)? Feb 23, 2015 at 18:52
  • I meant for the section number to be flush left with the left margin. As for the lines, I meant how far the vertical line extends above the text, and how far it extends below. There is only one line, but the wording was ambiguous. Feb 23, 2015 at 20:23
  • Please see my updated answer. Is it something like that what you had in mind? Feb 23, 2015 at 20:36

1 Answer 1

5

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.

4
  • I ran into a minor problem using this code. Please see the edit. Feb 23, 2015 at 22:41
  • @fctaylor25 Yes, you're right. I'll fix it and get back to you as soon as possible. Feb 23, 2015 at 23:15
  • @fctaylor25 Please see my updated answer. Since internal calculations are now involved, two compilations are needed to the code to stabilize. Feb 24, 2015 at 0:15
  • Thanks for the update. Would it be possible to make the number a different font than the heading? Also \usetikzlibrary{tikzmark,calc} triggers an emergency stop in the compiler I use. Feb 24, 2015 at 13:53

You must log in to answer this question.

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