1

I created the following 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}

This leads to the following PDF:

PDF header

What I want to achieve is to replace the red line (l. 9) by a color gradient: color gradient

I expect to define different colors (e.g., yellow at 0 %, orange at 25 %, red at 66 % and pink at 75 %).

How can I achieve such a "color gradient line"?

As this MWE is taken out of a large document, I am bound to titlesec and cannot replace this package. Nonetheless, if there is a way to (for example) include a tikz picture in the headrule which creates the color line, this would also be fine.

2 Answers 2

1

Expanding on samcarter's nice answer: For more complicated gradients with multiple stops, you can create your custom shading:

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

enter image description here


If you want to change the gradient somewhere in your document, you can just redefine the gradient mid-document. You need to make sure that the redefinition happens on the first page where the gradient should be used. The definition doesn't have to be the first thing that is typeset on this page. For example, the following draws a different gradient on the second page:

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

enter image description here enter image description here

Of course, you could also put the defintion inside the defintion of the page style

2

Might be overkill, but you could theoretically use 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}

enter image description here

3
  • I'd just use rectangle as it shortens the code. Also, the OP said that they would like to customize the colors, so maybe create some custom colors headrule left and headrule right that can then be changed throughout the document using \definecolor ... but this is just an idea =) Dec 13, 2023 at 16:25
  • 1
    @JasperHabicht you are of course right :) Feel free to post an answer with more flexible colours. Dec 13, 2023 at 16:27
  • I just saw that they even want more complicated gradients ... well ... they still could use a shading then, but probably would need to define a custom shading anyways. Dec 13, 2023 at 16:28

You must log in to answer this question.

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