0

I'd like to add symbols/rules/tikz drawing at the end of a title to obtain something like:

enter image description here

or

enter image description here

I tried to define something like:

\titleformat{\section}[runin]{\normalfont\Large\bfseries}{\thesection}{1em}{}[{\xrfill[1pt]{8pt}[secondaryColor]}\\]

(the {} seems to be necessary or it wont compile), but the spacing and indentation is wrong after the section:

enter image description here

the proper indentation and spacing being:

enter image description here

what is the proper way to get a proper spacing with \titlesec?

MWE: Note that for the tikz drawing, I've no idea if it's the best way to proceed (I use 3 tikz pictures, 2 with remember picture spaced with hfill, and one overlay in charge of the drawing), but I can't find any better way to get the position of the start/end of the current line. Let me know if you know one.

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\usepackage{lipsum}

\newcommand{\mytikzDrawing}{% % Ugly trick to find start/end of line... happy to hear better solutions. \begin{tikzpicture}[remember picture] \node[inner sep=0pt] (startPosition) {\phantom{X}}; \end{tikzpicture}\hfill\begin{tikzpicture}[remember picture] \node[inner sep=0pt] (endPosition) {\phantom{X}}; \end{tikzpicture}% \begin{tikzpicture}[remember picture,overlay] % \node[fill=blue!50,minimum width=\linewidth,minimum height=3mm] {AAA}; \fill[left color=red!30, right color=yellow!30] (startPosition.north west) -- (endPosition.north east) -- (endPosition.south east) -- (startPosition.south west) -- cycle; \draw[decorate,decoration=snake,draw=blue!50] (startPosition.east) -- (endPosition.west); \end{tikzpicture}% }

\usepackage{verbatim} \usepackage{xhfill} % To draw color \hrulefill https://tex.stackexchange.com/a/155960/

\usepackage{xcolor} \definecolor{secondaryColor}{RGB}{255,236,209}

\usepackage{titlesec} %% Bad spacing after: %\titleformat{\section}[runin]{\normalfont\Large\bfseries}{\thesection}{1em}{}[{\xrfill[1pt]{8pt}[secondaryColor]}\] % or with the tikz version: % \titleformat{\section}[runin]{\normalfont\Large\bfseries}{\thesection}{1em}{}[\ \mytikzDrawing\]

\begin{document}

Here is what I get when using nothing:

\section{Hello}

I'd prefer to get something like that (the problem being the spacing):\

\noindent{\normalfont\Large\bfseries 2 \hspace{.3em} A simple version \xrfill[1pt]{8pt}[secondaryColor]}\

Or something like that:

\noindent{\normalfont\Large\bfseries 3 \hspace{.3em} A complex version \mytikzDrawing{}}

\lipsum[1]

\end{document}

tobiasBora
  • 8,684

1 Answers1

2

Thanks to Werner's comment pointing to this question, I managed to understand how to do. The idea is to load the option explicit in the package, which forces the user to add #1 where the title is supposed to appear, and to put the tikz code in the pre-text (together with #1) instead of in the post-text. That way, the shape runin is not needed, and the spacing is directly the expected one:

\usepackage[explicit]{titlesec}
\titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}{#1\hrulefill}

For a complete example with tikz using linegoal to simplify the process to determining the remaining part of the line (see commented code for colored line):

enter image description here

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\usepackage{linegoal} % \linegoal gives the remaining space on current line
\usepackage{tabularx}
\usepackage{lipsum}
\usepackage{tcolorbox}
\newlength{\remDim} % Dimension that remains after the section title
\newcommand{\mytikzDrawing}{%
  \setlength\remDim{\linegoal}% Store the remaining dimension to draw a node of the appropriate width
  \begin{tikzpicture}%
    \node[minimum width=\remDim,minimum height=7pt,inner sep=0pt,fill,left color=red!30, right color=yellow!30] (mainbox) {};
    \draw[decorate,decoration=snake,draw=blue!50] (mainbox.east) -- (mainbox.west);
  \end{tikzpicture}%
}

\usepackage{xhfill} % To draw color \hrulefill https://tex.stackexchange.com/a/155960/ \usepackage{xcolor} \definecolor{secondaryColor}{RGB}{255,236,209}

\usepackage[explicit]{titlesec} %% Rule version: % \titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}{#1\hrulefill} %% Colored rule version: % \titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}{#1\xrfill[1pt]{8pt}[secondaryColor]} %% Tikz version: \titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}{#1 \mytikzDrawing}

\begin{document}

\section{A first section}

\lipsum[1]

\section{A second section}

\lipsum[1]

\end{document}

tobiasBora
  • 8,684