2

I wish to create a fancy line divider using tikz, to resemble the one shown below. enter image description here

The divider is basically a thin grey line with a larger rectangle on the upper side, such that the length of the rectangle is exactly equal to the length of the section title below it. I really have no idea how I can achieve this in LaTeX, and it seems like it may be rather complicated. Nevertheless, I would really appreciate some input.

\documentclass[11pt,a4paper]{article}
\usepackage{xcolor}
\usepackage[explicit]{titlesec}

%Underlining ruler for subsections
\titleformat{\section}
{\normalfont\Large\bfseries}
{}
{0em}
{#1}
[{\color{gray!50}\titlerule[0.8pt]}]

\begin{document}

\section{A test section}
Text text text


\end{document}
2
  • Please show us what you've got so far (MWE).
    – TeXnician
    Commented Nov 30, 2017 at 19:06
  • I've added a basic starting point. Perhaps you can help?
    – wrb98
    Commented Nov 30, 2017 at 19:15

2 Answers 2

5

Option calcwidth sets the register \titlewidth to the longest line in the title and is then the width for the thicker line. The thinner line follows with width \linewidth.

The rules are set before the title using \vadjust pre (keyword pre invented by pdfTeX and also implemented in XeTeX/LuaTeX).

TikZ is not necessary, the rules are drawn with \hrule of plain TeX.

Example:

\documentclass[11pt,a4paper]{article}
\usepackage{xcolor}
\usepackage[calcwidth]{titlesec}

%Underlining ruler for subsections
\titleformat{\section}%
  [hang]% shape
  {\normalfont\Large\bfseries}% format
  {}% label
  {0mm}% sep
  {%
    \vadjust pre{%
      \color{gray!50}%
      \hrule width \titlewidth height 4pt\relax
      \hrule width \linewidth height .4pt\relax
    }%
  }% before-code

\begin{document}

\section{A test section}
Text text text

\end{document}

Result

2
  • Thank you! Surprisingly more simple than I thought it would be.
    – wrb98
    Commented Nov 30, 2017 at 19:49
  • I've been using this divider for a year now and I've noticed that the vertical spacing between the title of the section and the bar is inconsistent. For example, try putting two sections one after the other. Is there a quick fix for this?
    – wrb98
    Commented Nov 19, 2018 at 18:46
2

This is a conceivable way of adjusting the length of the gray bar to the length of the text. As requested in the title, it makes use of tikz.

\documentclass{article}
\usepackage{tikz}
\usepackage{calc}
\usepackage{lipsum}
\usepackage[explicit]{titlesec}
\usepackage[titles]{tocloft}
\newlength{\marmotlength}
\setlength{\marmotlength}{2.7cm}
\newcommand\myfancysection[1]{\addtolength\marmotlength{\widthof{\Large{\textbf{#1}}}}%
\typeout{feep \the\marmotlength}
\tikz[baseline=(CHAP.base),very thick]{
  \draw[use as bounding box,draw=white,fill=white] (0,0) rectangle (2cm,2cm);
  \node[fill=white,minimum width=1.5cm,outer sep=0pt,rounded corners] (CHAP) at
  (1cm,\baselineskip+1ex) {\Large{\textbf{\textsf{\thesection}}}};
  \draw[black, line width=1pt](2cm,1.2cm) -- (\textwidth,1.2cm);
  \draw[gray!50!, line width=2mm](2cm,1.2cm) -- (\marmotlength,1.2cm);
}}

\titleformat{\section}
  {\normalfont}{\myfancysection{#1}}{1em}{\Large{\textbf{#1}}}

\begin{document}

\section{Blabla}

\lipsum[1]

\section{Blablablabla}

\lipsum[2]

\end{document}

enter image description here

You must log in to answer this question.

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