[Tex/LaTex] Customizing \section in moderncv

moderncvsectioning

I'd like to customize my moderncv a bit. For my sections I want a color fade in the headings. For this purpose I got this code in Latex:

\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}

customized color fade in section

I tried 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 it in my moderncv instead of \section?

This is how the \section command is defined in moderncv:

\renewcommand*{\section}[1]{%
  \par\addvspace{2.5ex}%
  \phantomsection{}% reset the anchor for hyperrefs
  \addcontentsline{toc}{section}{#1}%
  \strut\sectionstyle{#1}%
  {\color{color1}\hrule}%
  \par\nobreak\addvspace{1ex}\@afterheading}

where \sectionstyle is again an own command (sorry I don't have access right now to the files of moderncv)

Best Answer

Here's one possibility:

\documentclass{moderncv}
\usepackage{tikz}
\usetikzlibrary{fadings,patterns,positioning,fit,calc}

\tikzset{
  zero sep/.style = {inner sep=0pt, outer sep=0pt},
}

\moderncvstyle{classic}

\makeatletter
\renewcommand\section[1]{%
  \par\addvspace{2.5ex}%
  \phantomsection{}% reset the anchor for hyperrefs
  \addcontentsline{toc}{section}{#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) {\strut#1\strut};
    \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}
    \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}%
    \par\nobreak\addvspace{1ex}\@afterheading%
}
\makeatother  


\firstname{John}
\lastname{Doe}

\begin{document}

\section{First section}
Some text
\section{Second section with a long title to see what happens when spanning more than one line}
Some text
\section{Short}
Some text

\end{document}

The resulting document:

enter image description here

Notice, however, that the code you are trying to use will produce unexpected results for long titles (see what happens with the long title for the second section).