Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.
share|improve this question
    
Please add a minimal example that illustrates your problem. –  Matthew Leingang Apr 13 '11 at 11:46
    
@Matthew: I added 3 examples to illustrate. –  ℝaphink Apr 13 '11 at 12:10
    
that's very helpful, thanks! –  Matthew Leingang Apr 13 '11 at 12:28
    
I don't think that this has anything to do with the tikzpicture (as an overlay picture takes no space). The problem seems to be that titlesec adds an (empty) paragraph if the optional argument is nonempty. –  Caramdir Apr 13 '11 at 16:44
add comment

2 Answers 2

up vote 2 down vote accepted

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
}
share|improve this answer
    
I had tried using \monthtab in the before argument already. It doesn't cause the vertical space issue, but it forces the text to cut earlier on long lines, which is a problem on some of my pages where section names are long. All in all, I think your solution of using -\baselineskip is the cleanest, as it prevents me from guessing the height of the added line. Thank you. –  ℝaphink Apr 13 '11 at 22:04
    
@Raphink: It works for me: i.imgur.com/Uz7s5.png. Note that the \vspace is before \monthtab (I think the problem is that tikzpicture always leaves vertical mode (i.e. starts a paragraph); thus the \vspace must be before it). –  Caramdir Apr 13 '11 at 22:12
    
When I call \monthtab from before with the long title I have above (Toutes les promesses de Dieu), then the last word (Dieu) ends up on a second line. This doesn't happen when I use it in the after parameter. –  ℝaphink Apr 13 '11 at 22:19
    
@Raphink: Sorry, I misunderstood. See the extended answer for why this happens and how to solve the problem. –  Caramdir Apr 13 '11 at 22:24
    
3 newlines even in my complete code (since the one in my example was stripped to make it shorter). Thanks, that works great, and I could now get rid of the after parameter. –  ℝaphink Apr 13 '11 at 22:38
add comment

Placing it in \rlap{...} or \llap{...} would make it not take any official space. It simply laps to the right or left. However, this might overprint some of the following text.


Update

Ok, now with your examples I can see what's the problem is. The \rlap and \llap only take care about the horizontal space, not the vertical one. You would need the vertical versions for it. However, because you are using tikz you can do that using its commands instead. Simply make sure, that the picture has no vertical space, at best also no horizontal one.

Try to add \useasboundingbox (0,0); at the beginning of the picture where (0,0) here stands for the center of it. This way the picture has a size of 0 in both directions and the drawing material simply laps over in all directions.

share|improve this answer
    
Thanks. Somehow, it doesn't seem to work. Maybe the fact of calling []%after in \titleformat already allocates a vertical space, even if the contents use \rlap or \llap? –  ℝaphink Apr 13 '11 at 11:09
    
@Raphink: Does it make no difference at all? –  Martin Scharrer Apr 13 '11 at 11:32
    
@Martin: Not a difference that my eyes can see. –  ℝaphink Apr 13 '11 at 12:04
    
@Raphink: See my updated answer. –  Martin Scharrer Apr 13 '11 at 12:35
    
@Martin: you mean useasboundingbox, not useasboudingbox I guess. I've put it between the \begin{tikzpicture} and \node lines, but it doesn't fix the issue. The space is actually even bigger with useasboundingbox, as the paragraph in italic only begins under the bottom of the tab picture strictly now. –  ℝaphink Apr 13 '11 at 12:40
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.