在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.我嘗試重新定義它moderncvmoderncvstyleclassic但它仍然給我錯誤。你知道我必須做什麼才能在我的 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}

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

產生的文檔:

在此輸入影像描述

但請注意,您嘗試使用的程式碼將為長標題產生意外的結果(請參閱第二部分的長標題會發生什麼情況)。

相關內容