5

I want to define a special sectioning type for my CV. But I have problem which I can't figure out myself. I got the following code which produces the section heading with a color gradient how I would like it to be. Using it for the first section is working fine, but for the following sections I get always the same result which was written in the first section. The following code will illustrate the problem:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings,patterns,positioning,fit}
\tikzset{
  zero sep/.style = {inner sep=0pt, outer sep=0pt},
}
\begin{document}
  \newsavebox{\tempbox}
  \newcommand\tikzsection[1]{%
    \begin{tikzfadingfrompicture}[name=tikzsection]
      \node [minimum width=50mm, minimum height=2.5mm, fill=white, zero sep] (box node) {};
      \node (text node) [text=white, right=10pt of box node.east, anchor=west,  zero sep] {\normalfont \Large \bfseries #1};
      \node [fit=(box node.north west) (text node.south east) (text node.north west) (box node.south east)] {};
    \end{tikzfadingfrompicture}
    \begin{lrbox}{\tempbox}%
      \begin{tikzpicture}
        \node [text width=50mm, minimum height=2.5mm, fill=white, zero sep] (boxnode) {};
        \node [text=white, right=10pt of boxnode.east, anchor=west,  zero sep] (textnode) {\normalfont \Large \bfseries #1};
        \node (fitnode) [fit=(boxnode.north west) (textnode.south east) (textnode.north west) (boxnode.south east)] {};
        \shade[path fading=tikzsection, fit fading=false,left color=blue, right color=black]
        (fitnode.north west) rectangle (fitnode.south east);
      \end{tikzpicture}%
    \end{lrbox}
    % Now we use the fading in another picture:
    \section{\usebox\tempbox{}}%
  }

  \tikzsection{First section}
  Some text
  \tikzsection{Secoooooond section}
  Some text
\tikzsection{Short}
  Some text


\end{document}

Can someone figure out for me what is going wrong here? Why is the section titled as "Secoooooond section" rendered as "First section"?

3 sections

\listfilesgives me the following list:

  • article.cls 2007/10/19 v1.4h Standard LaTeX document class
  • size10.clo 2007/10/19 v1.4h Standard LaTeX file (size option)
  • tikz.sty 2013/12/13 v3.0.0 (rcs-revision 1.142)
  • pgf.sty 2013/12/18 v3.0.0 (rcs-revision 1.14)
  • pgfrcs.sty 2013/12/20 v3.0.0 (rcs-revision 1.28)
  • everyshi.sty 2001/05/15 v3.00 EveryShipout Package (MS)
  • pgfrcs.code.tex pgfcore.sty 2010/04/11 v3.0.0 (rcs-revision 1.7)
  • graphicx.sty 1999/02/16 v1.0f Enhanced LaTeX Graphics (DPC,SPQR)
  • keyval.sty 1999/03/16 v1.13 key=value parser (DPC)
  • graphics.sty 2009/02/05 v1.0o Standard LaTeX Graphics (DPC,SPQR)
  • trig.sty 1999/03/16 v1.09 sin cos tan (DPC)
  • graphics.cfg 2010/04/23 v1.9 graphics configuration of TeX Live
  • xetex.def 2013/04/29 v0.96 LaTeX color/graphics driver for XeTeX (RRM/JK)
  • pgfsys.sty 2013/11/30 v3.0.0(rcs-revision 1.47)
  • pgfsyssoftpath.code.tex 2013/09/09 (rcs-revision 1.9)
  • pgfsysprotocol.code.tex 2006/10/16 (rcs-revision 1.4)
  • xcolor.sty 2007/01/21 v2.11 LaTeX color extensions
  • color.cfg 2007/01/18 v1.5 color configuration of teTeX/TeXLive
  • pgfcomp-version-0-65.sty 2007/07/03 v3.0.0 (rcs-revision 1.7)
  • pgfcomp-version-1-18.sty 2007/07/23 v3.0.0 (rcs-revision 1.1)
  • pgffor.sty 2013/12/13 v3.0.0 (rcs-revision 1.25)
18
  • Is this different from tex.stackexchange.com/questions/215895/… and/or tex.stackexchange.com/questions/215856/…?
    – cfr
    Dec 8, 2014 at 19:39
  • @cfr yes it is a different question. This is regarding another problem as described above Dec 8, 2014 at 19:43
  • 2
    @AdrianPfeifle The code provided does not produce the image provided... Dec 8, 2014 at 23:32
  • 1
    As @PaulGaborit says, the code you posted does not have the issue you show. I get the correct section titles for all 3 sections.
    – cfr
    Dec 8, 2014 at 23:57
  • 3
    @AdrianPfeifle: I suspect that one of your pacakges must be out of date. I'd suggest you add \listfiles just before \begin{document} and add the package versions to your question so we can determine the package that is responsible. Alternatively, update to TeXLive2014. Dec 11, 2014 at 4:31

1 Answer 1

2
+50

The workaround for XeLaTeX : use a different name for each tikzfadingfrompicture!

Third Version

(working with XeLaTeX and with the same features as second version.)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings,patterns,positioning,fit,calc}
\tikzset{
  zero sep/.style = {inner sep=0pt, outer sep=0pt},
}

\newcommand\tikzsection[1]{%
  \pgfmathsetmacro\randref{rand}
  \begin{tikzfadingfrompicture}[name=tikzsection \randref]
    \node[fill=white,anchor=south east,zero sep,minimum width=5cm,minimum height=2.5mm] (box node){};
    \node [text=white,anchor=base west,text depth=5pt,text height=12pt,zero sep,
    font=\normalfont\Large\bfseries,right=10pt of box node,
    text width=5.9cm,align=left] (text node) {#1};
    \node [fit={(box node)(text node)
      },zero sep] (myfit) {};
    \path let \p1=(myfit.south west), \p2=(myfit.north east), \n1={\x2-\x1}, \n2={\y2-\y1} in
    \pgfextra{\xdef\lenx{\n1} \xdef\leny{\n2}};
  \end{tikzfadingfrompicture}
  \section[#1]{%
    \begin{tikzpicture}[baseline=.5*5pt-.5*12pt]
      \path[path fading=tikzsection \randref, fit fading=false,left color=blue, right color=black]
      (-.5*\lenx,-.5*\leny) rectangle ++(\lenx,\leny);
    \end{tikzpicture}
  }
}
\pagestyle{empty}

\begin{document}
\tikzsection{First section}
Some text
\tikzsection{Secoooooond segtion}
Some text
\tikzsection{Short}
Some text

\end{document}

Second Version

Here is a new version:

  • with correct position of the tikz fading,
  • without a save box,
  • with care about depth and height of title,
  • with correct alignment of the baseline,
  • with a fixed length (15cm) for the title (and the fading).

enter image description here

\documentclass{article}
\usepackage[rgb]{xcolor}
\usepackage{tikz}
\usetikzlibrary{fadings,patterns,positioning,fit,calc}
\tikzset{
  zero sep/.style = {inner sep=0pt, outer sep=0pt},
}

\newcommand\tikzsection[1]{%
  \pgfmathsetmacro\randref{rand}
  \begin{tikzfadingfrompicture}[name=tikzsection \randref]
    \fill[white] (0,0) rectangle +(5cm,2.5mm);
    \node [text=white,anchor=base west,text depth=5pt,text height=12pt,zero sep,font=\normalfont\Large\bfseries,
    text width=10cm,align=left]
    (text node) at (5cm+10pt,0) {#1};
    \node [fit={(0,0)(text node)},zero sep] (myfit) {};
    \path let \p1=(myfit.south west), \p2=(myfit.north east), \n1={\x2-\x1}, \n2={\y2-\y1} in
    \pgfextra{\xdef\lenx{\n1} \xdef\leny{\n2}};
  \end{tikzfadingfrompicture}
  \typeout{lenx:\lenx}
  \typeout{leny:\leny}
  \section[#1]{%
    \begin{tikzpicture}[baseline=.5*5pt-.5*12pt]
      \path[path fading=tikzsection \randref, fit fading=false,left color=blue, right color=black]
      (-.5*\lenx,-.5*\leny) rectangle ++(\lenx,\leny);
    \end{tikzpicture}
  }
}

\begin{document}
\tikzsection{First section}
Some text
\tikzsection{Secoooooond segtion}
Some text
\tikzsection{Short}
Some text

\end{document}

First version

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings,patterns,positioning,fit}
\tikzset{
  zero sep/.style = {inner sep=0pt, outer sep=0pt},
}
\begin{document}


  \newsavebox{\tempbox}
  \newcommand\tikzsection[1]{%
    \begin{tikzfadingfrompicture}[name=tikzsection #1]
      \node [minimum width=50mm, minimum height=2.5mm, fill=white, zero sep] (box node) {};
      \node (text node) [text=white, right=10pt of box node.east, anchor=west,  zero sep] {\normalfont \Large \bfseries #1};
      \node [fit=(box node.north west) (text node.south east) (text node.north west) (box node.south east)] {};
    \end{tikzfadingfrompicture}
    \begin{lrbox}{\tempbox}%
      \begin{tikzpicture}
        \node [text width=50mm, minimum height=2.5mm, fill=white, zero sep] (boxnode) {};
        \node [text=white, right=10pt of boxnode.east, anchor=west,  zero sep] (textnode) {\normalfont \Large \bfseries #1};
        \node (fitnode) [fit=(boxnode.north west) (textnode.south east) (textnode.north west) (boxnode.south east)] {};
        \shade[path fading=tikzsection #1, fit fading=false,left color=blue, right color=black]
        (fitnode.north west) rectangle (fitnode.south east);
      \end{tikzpicture}%
    \end{lrbox}
    % Now we use the fading in another picture:
    \section[#1]{\usebox\tempbox{}}%
  }

  \tikzsection{First section}
  Some text
  \tikzsection{Secoooooond section}
  Some text
\tikzsection{Short}
  Some text


\end{document}
6
  • Perfect! Maybe you can still help me, because now I have two new questions. 1.) I would like to align the bars with a fixed length and the headings of the sections left justified (to the right side of the bars) Dec 11, 2014 at 13:05
  • 2.) is it possible to have a "fixed" fading? I mean now no matter how short or long the section heading is the last letter always has black color. But lets say I want the whole color fade to be fixed with starting with the left color at the left side of the bars and ending with the right color after 15 cm. So if a section heading is shorter than 15 cm the last letter has the color that the would occur for the fixed color fade at the position of the last letter Dec 11, 2014 at 13:06
  • I tried now to include this in my moderncv but obviously I can't just use it like that since the \section command is already defined in moderncv. I tried to redefine it in moderncv and moderncvstyleclassic but it still gives me errors. Do you know what I have to do to use what you did in my moderncv instead of \section? Dec 12, 2014 at 9:12
  • oh and I just realized when I was testing your new version I compiled with pdfLaTeX and it was working fine. But when I compile with XeLaTeX I can see only the section heading but not the bars to the left Dec 12, 2014 at 9:55
  • BTW im using XeLaTeX because I'm using fonts that are not available in pdfLaTeX Dec 12, 2014 at 10:24

You must log in to answer this question.

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