titlesec 中的颜色渐变线

titlesec 中的颜色渐变线

我创建了以下 MWE:

\documentclass[a4paper,11pt,oneside]{article}
\usepackage[a4paper,margin=2.5cm,marginparwidth=2cm]{geometry}
\usepackage[pagestyles]{titlesec}
\usepackage{hyperref}
\usepackage{eso-pic}

\newpagestyle{main}{
    % http://ctan.math.washington.edu/tex-archive/macros/latex/contrib/titlesec/titleps.pdf
    \renewcommand\makeheadrule{\color{red}\rule[-.3\baselineskip]{\linewidth}{0.55pt}}% TODO: This creates the line in "red" which should be changed to a line with custom color gradient
    \sethead{\color{green}\thesubsection~\subsectiontitle}%odd-left
            {}%odd-center
            {\color{blue}\sectiontitle}%odd-right
    \setfoot{This is the footer.}%footer-left
            {Footer center.}%footer-center
            {\thepage}%footer-right
}

\begin{document}
\pagestyle{main}

\section{Sectiontitle}
Test.
\subsection{Subsectiontitle}
Text.

\end{document}

这导致以下 PDF:

PDF 标题

我想要实现的是用颜色渐变替换红线(l.9): 颜色渐变

我希望定义不同的颜色(例如,0%为黄色,25%为橙色,66%为红色,75%为粉色)。

如何才能实现这样的“颜色渐变线”呢?

由于此 MWE 是从大型文档中提取出来的,因此我被限制titlesec并且无法替换此包。不过,如果有办法(例如)在创建颜色线的 headrule 中包含 tikz 图片,那么这也很好。

答案1

扩展samcarter 的回答很好:对于具有多个停止点的更复杂的渐变,您可以创建自定义阴影:

\documentclass[a4paper, 11pt, oneside]{article}
\usepackage[a4paper, margin=2.5cm, marginparwidth=2cm]{geometry}
\usepackage[pagestyles]{titlesec}

\usepackage{tikz}
\pgfdeclarehorizontalshading{my shading}{100bp}{
    color(0bp)=(yellow); 
    color(20bp)=(yellow); 
    color(40bp)=(orange); 
    color(60bp)=(red); 
    color(80bp)=(magenta);
    color(100bp)=(magenta)
}

\newpagestyle{main}{
    % http://ctan.math.washington.edu/tex-archive/macros/latex/contrib/titlesec/titleps.pdf
    \renewcommand\makeheadrule{%
        \tikz[baseline=.3\baselineskip]{
            \shade[shading=my shading] 
                (0,0) rectangle (\linewidth,0.55pt);
        }%
    }
    \sethead{\color{green}\thesubsection~\subsectiontitle} %odd-left
            {} %odd-center
            {\color{blue}\sectiontitle} %odd-right
    \setfoot{This is the footer.} %footer-left
            {Footer center.} %footer-center
            {\thepage} %footer-right
}

\begin{document}
\pagestyle{main}

\section{Sectiontitle}
Test.
\subsection{Subsectiontitle}
Text.

\end{document}

在此处输入图片描述


如果您想在文档中的某个地方更改渐变,只需在文档中间重新定义渐变即可。您需要确保重新定义发生在应使用渐变的第一页上。定义不必是此页面上排版的第一项。例如,以下内容在第二页上绘制了不同的渐变:

\documentclass[a4paper, 11pt, oneside]{article}
\usepackage[a4paper, margin=2.5cm, marginparwidth=2cm]{geometry}
\usepackage[pagestyles]{titlesec}

\usepackage{tikz}
\pgfdeclarehorizontalshading{my shading}{100bp}{
    color(0bp)=(yellow); 
    color(20bp)=(yellow); 
    color(40bp)=(orange); 
    color(60bp)=(red); 
    color(80bp)=(magenta);
    color(100bp)=(magenta)
}

\newpagestyle{main}{
    % http://ctan.math.washington.edu/tex-archive/macros/latex/contrib/titlesec/titleps.pdf
    \renewcommand\makeheadrule{%
        \tikz[baseline=.3\baselineskip]{
            \shade[shading=my shading] 
                (0,0) rectangle (\linewidth,1pt);
        }%
    }
    \sethead{\color{green}\thesubsection~\subsectiontitle} %odd-left
            {} %odd-center
            {\color{blue}\sectiontitle} %odd-right
    \setfoot{This is the footer.} %footer-left
            {Footer center.} %footer-center
            {\thepage} %footer-right
}

\begin{document}
\pagestyle{main}

\section{Sectiontitle}
Test.
\subsection{Subsectiontitle}
Text.

\newpage % page break here

\section{Sectiontitle}
Test.

\pgfdeclarehorizontalshading{my shading}{100bp}{
    color(0bp)=(cyan); 
    color(20bp)=(cyan); 
    color(40bp)=(blue); 
    color(60bp)=(violet); 
    color(80bp)=(magenta);
    color(100bp)=(magenta)
}

\section{Sectiontitle}
Test.

\end{document}

在此处输入图片描述在此处输入图片描述

当然,你也可以将定义放在页面样式的定义中

答案2

可能有点过头了,但理论上你可以使用 tikz:

\documentclass[a4paper,11pt,oneside]{article}
\usepackage[a4paper,margin=2.5cm,marginparwidth=2cm]{geometry}
\usepackage[pagestyles]{titlesec}
\usepackage{hyperref}
\usepackage{eso-pic}

\usepackage{tikz}

\newpagestyle{main}{
    % http://ctan.math.washington.edu/tex-archive/macros/latex/contrib/titlesec/titleps.pdf
    \renewcommand\makeheadrule{
      \begin{tikzpicture}[baseline=1.5ex]
      \shade[right color=orange,left color=yellow] (0,0) rectangle (\linewidth,2pt);
      \end{tikzpicture}
    }% TODO: This creates the line in "red" which should be changed to a line with custom color gradient
    \sethead{\color{green}\thesubsection~\subsectiontitle}%odd-left
            {}%odd-center
            {\color{blue}\sectiontitle}%odd-right
    \setfoot{This is the footer.}%footer-left
            {Footer center.}%footer-center
            {\thepage}%footer-right
}

\begin{document}
\pagestyle{main}

\section{Sectiontitle}
Test.
\subsection{Subsectiontitle}
Text.

\end{document}

在此处输入图片描述

相关内容