[Tex/LaTex] Calculating position in tikz

calculationspositioningtikz-pgf

I'm trying to do calculation to be used in tikz. Here is my current code:

\def\monthtabpos#1%
  {\ifthenelse{#1<7}
     {\dimexpr \numexpr -3*#1 \relax cm}
     {\dimexpr \numexpr -3*(#1-6) \relax cm}
  }        

\newcommand{\monthtab}[2]{
  \shorthandoff{;}
  \begin{tikzpicture}[remember picture,overlay]
    \node[yshift=\monthtabpos{#2},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}
}

The idea is that \monthtabpos calculates the vertical position of the picture. This code is then called from \titleformat to position a tab for sections:

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

When I build this code, I get:

LaTeX Warning: Unsynchronized `section' title on page 7.

! Argument of \XC@definec@lor has an extra }.
<inserted text> 
                \par 
l.2 \dvday{Créé à son image}

Note that if I replace yshift=\monthtabpos{#2} with yshift=-3*{#2}cm, it works fine (except I don't get the effect I want when #2 > 6), and that I have checked that \monthtabpos returns the expected string.

I have also tried defining a local macro in tikz with:

\newcommand{\monthtab}[2]{
  \shorthandoff{;}
  \begin{tikzpicture}[remember picture,overlay]
    \node let \tabpos = (\monthtabpos{#2}) in
      [yshift=\tabpos,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}
}

and it doesn't help.

Note that this does work (but is really ugly I think):

\newcommand{\monthtab}[2]{
  \shorthandoff{;}
  \ifthenelse{#2<7}{
  \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}
  }
  {
  \begin{tikzpicture}[remember picture,overlay]
    \node[yshift=18cm-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}
  }
}

Best Answer

I think the easiest way to calculate this is yshift={-3*(mod(#2-1,6)+1)*1cm}, which also works for chapters 13, 14, etc.

\mod(a,b) gives the remainder of the division of a by b. So mod(#2-1,6) will give 0,1,2,3,4,5,0,1,2,... for #2=1,2,3,...

See chapter 63.2 in the v2.10 TikZ manual for a list of functions that are available to do calculations within TikZ (for example it has ifthenelse).