[Tex/LaTex] Make tikz picture floating

floatspositioningtikz-pgf

I'm inserting a tikz picture in my section headings with \titleformat (see this question) and I would like it to not affect the spaces in the headings. Right now, what I do is that I compensate the introduction of the picture with a negative vspace:

\titleformat{name=\section,page=odd}[display]
  {\newpage\secstyle} % format
  {\large\makedate{\thesection}~\chaphead} % label
  {10pt} %sep
  {\vspace{-5mm}\filcenter\textls[-50]} %before
  [\monthtab{\chaphead}{\arabic{chapter}}\vspace{-1.2cm}] % after

\monthtab is the macro that inserts the picture to the right of the page. Is there a way to make it so that the picture inserted by \monthtab does not affect the spacing at all, making it float in the page?

Here are some examples. First, a section without a tab:

section without a tab

Then, a section with a tab and nothing to prevent the vertical space:

section with a tab, no vspace

Finally, when I add a negative \vspace to fix it (call that the target):

section with a tab and vspace

And here is a "minimal" example to reproduce the issue:

\documentclass[paper=6.14in:9.21in,pagesize=pdftex,10pt,DIV=15]{scrbook}
\usepackage{titlesec}
\usepackage{tikz}
\usepackage{microtype}

\newcommand{\monthtab}[2]{
  \begin{tikzpicture}[remember picture,overlay]
    \node[yshift=-3*{#2}cm,xshift=-0.5cm] at (current page.north east) {
      \tikz\shade[shading=axis,bottom color=white,top color=gray!50,shading angle=-90] 
        (0,0) rectangle (1cm,3cm) node[rotate=90,pos=0.5] {\Large\scshape #1};
    };
  \end{tikzpicture}
}

\newcommand{\secstyle}{\scshape\Huge}

% Tune section headings
\renewcommand\thesection{\arabic{section}}
\titleformat{name=\section,page=even}[display]
  {\newpage\secstyle} % format
  {} % label
  {10pt} %sep
  {\vspace{-5mm}\filcenter\textls[-35]} %before
\titleformat{name=\section,page=odd}[display]
  {\newpage\secstyle} % format
  {} % label
  {10pt} %sep
  {\vspace{-5mm}\filcenter\textls[-50]} %before
  [\monthtab{Chapname}{\arabic{chapter}}] % after
\titlespacing{\section}{0pt}{*}{*-2}

\begin{document}

\chapter{Janvier}
\section{This is a test section}

\begin{center}
Some text here
\end{center}

\section{This is another section}

\begin{center}
Some text here
\end{center}

\end{document}

I'm having a hard time making it shorter than this… Compare pages 2 and 3:

  • on page 2, the text almost touches the title;
  • on page 3, the tab to the right adds a space after the title.

Best Answer

The existence of anything nontrivial in the optional after parameter of \titleformat seems to cause titlesec to insert a new line. I don't know how to avoid that, but a simple \vspace{-\baselineskip} should undo the effect.

\titleformat{name=\section,page=odd}[display]
  {\newpage\secstyle} % format
  {} % label
  {10pt} %sep
  {\vspace{-5mm}\filcenter\textls[-50]} %before
  [\monthtab{Chapname}{\arabic{chapter}}\vspace{-\baselineskip}] % after

Alternatively, you can add the \monthtab to the before argument:

\titleformat{name=\section,page=odd}[display]
  {\newpage\secstyle} % format
  {} % label
  {10pt} %sep
  {\vspace{-5mm}\monthtab{Chapname}{\arabic{chapter}}%
   \filcenter\textls[-50]}

The current \monthtab macro contains two uncommented newlines, both of which add a space to the text. To avoid this, add a comment character at the end of the line, like

\newcommand{\monthtab}[2]{% <--- comment here
  \begin{tikzpicture}[remember picture,overlay]
      \node[yshift={ -3*(mod(#2-1,6)+1)*1cm},xshift=-0.5cm] at (current page.north east) {
      \tikz\shade[shading=axis,bottom color=white,top color=gray!50,shading angle=-90] 
        (0,0) rectangle (1cm,3cm) node[rotate=90,pos=0.5] {\Large\scshape #1};
    };
  \end{tikzpicture}% <--- comment here
}
Related Question