更改標題行的顏色

更改標題行的顏色

問題的答案fancyhdr - 更改頁腳頂部的水平線顏色並不完全令人滿意,因為它改變了標題行的位置,例如本例。

\documentclass{article}

\usepackage{xcolor}
\usepackage{fancyhdr}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\headrule}{\hbox to\headwidth{\color{red}\leaders\hrule height \headrulewidth\hfill}}
\fancyhead[LE,RO]{Hello}
\pagestyle{fancy}

\begin{document}
    hello
\end{document}

是否可以僅更改線條的顏色而不更改其位置?

答案1

下面我將原始內容儲存\headrule在 中\oldheadrule,然後新增\color{<colour>}到一個新定義的\headrule僅呼叫\oldheadrule.該方法類似於etoolbox\pretocmd.

在此輸入影像描述

\documentclass{article}

\usepackage{fancyhdr,xcolor}

\let\oldheadrule\headrule% Copy \headrule into \oldheadrule
\renewcommand{\headrule}{\color{red}\oldheadrule}% Add colour to \headrule
\renewcommand{\headrulewidth}{0.5pt}
\fancyhead{Hello}
\pagestyle{fancy}

\begin{document}

Lorem ipsum\ldots

\clearpage

Lorem ipsum\ldots

\end{document}

相關內容