我想在页眉中添加一个彩色框,但这样做会完全破坏我的页面布局,页脚会随处出现。还请注意,我如何粗暴地对齐了左右页眉……并且我必须添加一些空间才能看到任何东西。
我非常感谢你的帮助。
\documentclass[11pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{esvect}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage[top=4cm, bottom=4cm, left=3cm, right=3cm]{geometry}
\usepackage{enumerate}
\usepackage{graphicx}
\usepackage{fancyhdr,lastpage}
\usepackage{rotating}
\pagestyle{fancy}
\usepackage{array}
\usepackage[table]{xcolor}
\usepackage{multirow}
\usepackage{hhline}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\usepackage{fancybox}
\usepackage[most]{tcolorbox}
\usepackage{yhmath}
\usepackage{fancyhdr,lastpage}
\usepackage{wrapfig}
\usepackage{eso-pic}
\pagestyle{fancy}
\usepackage{tikz,pgf}
\usepackage{lipsum}
\tcbset{longtitle/.style={%
title={#1},
before upper={\begin{tabular}{@{}l@{}}\phantom{#1}\\[\the\dimexpr-\ht\strutbox-\dp\strutbox]},
after upper={\end{tabular}}}}
\newtcbox{\LongTitleBox}[1][]{longtitle={\Large \textbf{Test Header}},colbacktitle=black,coltitle=white,#1,colback=gray!20,boxrule=1pt}%
\newtcbox{\LongTitleBoxResult}[1][]{enhanced,longtitle={Final Result},colbacktitle=white,coltitle=black, #1}%
\fancypagestyle{firstpage}
{%
\fancyhead[L]{\LARGE \LongTitleBox{Test Title}}
\fancyhead[R]{Autor Name\\[0.5cm]}%
}
\makeatletter
\newcommand{\mybox}[1]{%
\setbox0=\hbox{#1}%
\setlength{\@tempdima}{\dimexpr\wd0+12pt}%
\begin{tcolorbox}[colback=gray!10,colframe=black,boxrule=0.5pt,arc=4pt,
left=6pt,right=6pt,top=6pt,bottom=6pt,boxsep=0pt,width=\@tempdima]
#1
\end{tcolorbox}
}
\fancyhead{}
\fancyfoot[R]{\vspace{-0.75cm}\mybox{Some text}}
\fancyfoot[L]{\today}
\cfoot{}
\nointerlineskip
%%% magic code starts
\mathcode`*=\string"8000
\begingroup
\catcode`*=\active
\xdef*{\noexpand\textup{\string*}}
\endgroup
%%% magic code ends
\renewcommand{\labelitemi}{$-$}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
\usepackage{showframe}
\begin{document}
\thispagestyle{firstpage}
\vspace*{2.5cm}
\lipsum
\end{document}
答案1
这里显示不正常的主要原因是长度设置错误\headheight
。
环境tcolorbox
有一些跳过之前和之后,没有使用nobeforeafter
报告headheight
应该是的选项90pt
,但可以减少到70pt
该值或略低于该值。
但是我没有解决代码中的其他问题。
\documentclass[11pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{esvect}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage[top=4cm, bottom=4cm, left=3cm, right=3cm,,headheight=70pt]{geometry}
\usepackage{enumerate}
\usepackage{graphicx}
\usepackage{rotating}
\usepackage{array}
\usepackage[table]{xcolor}
\usepackage{multirow}
\usepackage{hhline}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\usepackage{fancybox}
\usepackage[most]{tcolorbox}
\usepackage{yhmath}
\usepackage{fancyhdr,lastpage}
\pagestyle{fancy}
\usepackage{wrapfig}
\usepackage{eso-pic}
\pagestyle{fancy}
\usepackage{tikz,pgf}
\usepackage{lipsum}
\tcbset{longtitle/.style={%
title={#1},
nobeforeafter,
before upper={\begin{tabular}{@{}l@{}}\phantom{#1}\\[\the\dimexpr-\ht\strutbox-\dp\strutbox]},
after upper={\end{tabular}}}}
\newtcbox{\LongTitleBox}[1][]{longtitle={\Large \textbf{Test Header}},colbacktitle=black,coltitle=white,#1,colback=gray!20,boxrule=1pt}%
\newtcbox{\LongTitleBoxResult}[1][]{enhanced,longtitle={Final Result},colbacktitle=white,coltitle=black, #1}%
\fancypagestyle{firstpage}
{%
\fancyhead[L]{\LARGE \LongTitleBox{Test Title}}
\fancyhead[R]{Autor Name\\[0.5cm]}%
}
\makeatletter
\newcommand{\mybox}[1]{%
\setbox0=\hbox{#1}%
\setlength{\@tempdima}{\dimexpr\wd0+12pt}%
\begin{tcolorbox}[colback=gray!10,colframe=black,boxrule=0.5pt,arc=4pt,
left=6pt,right=6pt,top=6pt,bottom=6pt,boxsep=0pt,width=\@tempdima]
#1
\end{tcolorbox}
}
\fancyhead{}
\fancyfoot[R]{\vspace{-0.75cm}\mybox{Some text}}
\fancyfoot[L]{\today}
\cfoot{}
\nointerlineskip
%%% magic code starts
\mathcode`*=\string"8000
\begingroup
\catcode`*=\active
\xdef*{\noexpand\textup{\string*}}
\endgroup
%%% magic code ends
\renewcommand{\labelitemi}{$-$}
\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}
\usepackage{showframe}
\begin{document}
\thispagestyle{firstpage}
\vspace*{2.5cm}
\lipsum
\end{document}