fancyhdr 部分名称太长,左右两边的线条

fancyhdr 部分名称太长,左右两边的线条

我正在使用 fancyhdr 作为标题。问题是,我的一些部分名称太长,看起来很愚蠢。我附上了一张截图。

  1. 那么我该如何正确实现这一点呢?这样它看起来就不再那么愚蠢了。

  2. 然后我的第二个问题是:我想要左边和右边(而不是上面)有一条线,我还附加了一张截图,显示它应该是什么样子(它们应该与它下面的线相同)。

这是我的代码:

\documentclass{book}
\usepackage{geometry}
\geometry{left=4cm,right=3cm, top=2cm, bottom=2cm} 
\usepackage[pagestyles]{titlesec}
\titlespacing*{\chapter}{0pt}{-30pt}{20pt}
\titleformat{\chapter}[display]{\color{headercolor}\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{20pt}{\Huge}

\titleformat{\section}
{\color{headercolor}\normalfont\Large\bfseries}{\thesection}{1em}{}

\titleformat{\subsection}
{\color{headercolor}\normalfont\Large\bfseries}{\thesubsection}{1em}{}

\titleformat{\subsubsection}
{\color{headercolor}\normalfont\large\bfseries}{\thesubsubsection}{1em}{}

\titleformat{\paragraph}
{\color{headercolor}\normalfont\normalsize\bfseries}{\theparagraph}{1em}{}
\usepackage[ngerman]{babel}
\usepackage{mathptmx}
\usepackage{helvet}
\usepackage{wallpaper}
\usepackage{color}
\usepackage[final]{pdfpages} 
%,bookmarksopenlevel={1}
%\usepackage[hidelinks,bookmarks=true,bookmarksopen=true,bookmarksnumbered=true,colorlinks=true,linkcolor=black,]{hyperref}
\usepackage[colorlinks,linkcolor=black,bookmarksopen=false,
hypertexnames=TRUE,pdfpagelabels=true]{hyperref}[2011/02/05]
\hypersetup{ 
  pdftitle={},
%  pdfauthor={\textcopyright },
  pdfsubject={Statistics book}, 
  pdfkeywords={}, 
  }
\usepackage{xcolor,bookmark}
\usepackage{scrextend}
\usepackage{titlepic}
\usepackage{shorttoc}
\usepackage{courier}
%\usepackage[titles]{tocloft}
\usepackage{type1cm}         
\usepackage{zref-abspage}
\usepackage{makeidx}         
\usepackage{graphicx}        
\usepackage{multicol}       
\usepackage[bottom]{footmisc}
\usepackage{tocstyle}
\usetocstyle{allwithdot} 
%\usepackage{thmbox}
\usepackage{shadethm}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{marginnote}
%\usetocstyle{KOMAlike}
\usepackage{wrapfig}
\usepackage{paralist}
\usepackage{amssymb}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{ulem}
\usepackage{fancyhdr}
%\usepackage{sectsty}

\definecolor{headercolor}{rgb}{0.6941,0.6745,0.4863}


\setlength{\headheight}{21pt}%

\begin{document}
\mainmatter%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\pagestyle{fancy}
\fancyhf{}
%\fancyhead[L]{\parbox{0.7\textwidth}{\textbf{ \textcolor{headercolor}\leftmark\text{ }\textcolor{headercolor}{\rightmark}}}}
\fancyhead[L]{ \textcolor{headercolor}\leftmark \text{ }\textcolor{headercolor}{\rightmark}}%\thesection
\fancyhead[R]{\textcolor{headercolor}{\thepage}}
%\renewcommand{\headrule}{\vbox to 0pt{\hbox
%to\headwidth{\textcolor{headercolor}{\hrulefill}}\vss}} 
%%\renewcommand{\headrulewidth}{2pt}

 \renewcommand{\headrulewidth}{2pt}% 2pt header rule
  \renewcommand{\headrule}{\vbox to 0pt{\hbox
    to\headwidth{\textcolor{headercolor}{\rule{\linewidth}{\headrulewidth}}}\vss}} 

% here comes the sections part, I commented to have a running sample:

%\include{part1} 
%\include{chapter1}

% There I have those sections like
\section{very long text, really long, too long text, this looks not good but the names have to be like they are, yeah, really long, ..... ..... ..... ..... ..... ..... ..... ..}

text text\\text\\text\\text\\text and so on

\end{document}

以下是截图:问题 1:

在此处输入图片描述

在此处输入图片描述

问题2:

在此处输入图片描述

答案1

对于第一个问题,最好提供一个简短的标题和章节标题:

\section[Short section heading]{This is the equivalent long title that is typeset over several lines which is quite annoying. On a second note the title should probably be shorter}

我强烈建议限制您的长标题,标题就是标题,而不是独立的描述。

答案2

\documentclass{article}
\usepackage{fancyhdr}
\usepackage{calc}
\usepackage{color}

\usepackage{lipsum}

\setlength{\headheight}{21pt}%
\fancyhf{}
\fancyhead[L]{\textcolor{red}{\hspace{2mm}\leftmark\rightmark\hfill\thepage\hspace{2mm}}}
\renewcommand{\headrule}{\vspace{-\headheight}\textcolor{red}{\vrule height \headheight width 4pt\relax\rule{\textwidth-8pt}{4pt}\vrule height \headheight width 4pt\relax}}


\begin{document}
\pagestyle{fancy}

\section[Short section name]{\lipsum[1]}
\lipsum[1]
\end{document}

在此处输入图片描述

相关内容