在 moderncv 中自定义 \section

在 moderncv 中自定义 \section

我想对我的 moderncv 进行一些自定义。对于我的部分,我希望标题的颜色淡化。为此,我在 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}

自定义颜色淡入部分

我尝试将其包含在我的 中moderncv,但显然我不能就这样使用它,因为该\section命令已在 中定义moderncv。我尝试在 中重新定义它moderncv,但moderncvstyleclassic仍然出现错误。你知道我必须做什么才能在我的 moderncv 中使用它而不是 吗\section

\section该命令在 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}

其中\sectionstyle又是一个自己的命令(抱歉,我现在无权访问 的文件moderncv

答案1

有一种可能性是:

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

生成的文档:

在此处输入图片描述

但是请注意,您尝试使用的代码将对长标题产生意外的结果(请查看第二部分的长标题会发生什么)。

相关内容