页眉规则上方和下方的文本,悬挂页眉文本

页眉规则上方和下方的文本,悬挂页眉文本

我试图在每个页面上创建一个标题,并在标题规则的上方和下方添加文本,使其看起来像这样:

                                  header text above line
--------------------------------------------------------
                                  header text below line

[body of page]

这可能吗fancyhdr?或者还有其他方法可以做到这一点吗?

我意识到这可能不美观,但我试图模仿另一个文档(不是用 LaTeX 制作的)。

答案1

尝试一下,使用 fancyhdr:

\pagestyle{fancy}
\fancyhead[LE,LO]{~\\\hrulefill\\~}
\fancyhead[CE,CO]{~\\\hrulefill\\~}
\fancyhead[RE,RO]{text above\\\hrulefill\\text below}

如果需要,可以左右切换。您需要调整页眉高度和页眉中的行距以及相关尺寸。

答案2

tabular您可以使用具有单个aragraph 列的 来设置一个标题项p,该列的宽度为\textwidth

在此处输入图片描述

\documentclass{article}

\usepackage{fancyhdr,lipsum}

\pagestyle{fancy}
\fancyhf{}% Clear header/footer
\renewcommand{\headrulewidth}{0pt}% Remove header rule
\fancyhead[C]{%
  \begin{tabular}{ @{} p{\textwidth} @{} }
    \hfill Header text above line \\
    \hline
    \hfill Header text below line
  \end{tabular}}
\setlength{\headheight}{24.4pt}% Should be at least 24.4pt

\begin{document}

\lipsum

\end{document}

答案3

这是一个具有titlepstabular环境和的解决方案booktabs

\documentclass[12pt,twoside, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{titleps}
\usepackage{array, booktabs, graphicx}
    \usepackage{lipsum}
\newpagestyle{mine}{\renewcommand\arraystretch{0.5}%
\sethead[][\begin{tabular}{@{}>{\raggedright\small\arraybackslash}p{\textwidth}@{}}header text above line\\\midrule header text below line\end{tabular}][]%
{}{\begin{tabular}{@{}>{\raggedleft\small\arraybackslash}p{\textwidth}@{}}header text above line\\\midrule header text below line\end{tabular}}{}
\setfoot{}{\thepage}{}
}
\pagestyle{mine}

\begin{document}

\lipsum

\end{document} 

在此处输入图片描述

答案4

这是一个非常有趣的问题,我想为文档类提供一个全面的解决方案。article此解决方案使用包geometry、、和,乍一看很复杂,但相信我,它很好,而且fancyhdrpgfscrextendtextpos干净的并且无需调整,即使您更改页面几何形状

这些包的用途是,geometry用于设置页面几何形状,fancyhdr用于生成页眉,pgf用于定义长度,scrextend用于确定当前页面是偶数页还是奇数页,最后textpos用于放置文本。

我还在左上角添加了文本块。

以下是代码

% !TEX encoding = UTF-8 Unicode
% !TEX TS-program = xelatex
\documentclass[UTF8]{article}

\usepackage[b4paper, twoside]{geometry}
%% you can set page to be 'a4paper' or 'c5paper' or 'legalpaper' or whatever provided by 'geometry', no other adjustment is needed
%% even and odd page are set to have different margin size, you can set margin size to be equal by removing 'twoside', no other adjustment is needed
\usepackage{lipsum} %% generate random texts

\usepackage{fancyhdr} %% produce header

\usepackage{pgf} %% define length
\usepackage[absolute, overlay]{textpos} %% put text blocks at absolute position
\usepackage{scrextend} %% check if current page is even or odd

\pgfmathsetlengthmacro{\texttop}{1.0 in + \voffset + \topmargin + \headheight + \tabcolsep}
%% this is the very top of the body

\pgfmathsetlengthmacro{\textrightodd}{1.0 in + \oddsidemargin + \hoffset + \textwidth}
%% this is the right edge of the body for odd page
\pgfmathsetlengthmacro{\textrighteven}{1.0 in + \evensidemargin + \hoffset + \textwidth}
%% this is the right edge of the body for even page
\pgfmathsetlengthmacro{\textleftodd}{1.0 in + \oddsidemargin + \hoffset}
%% this is the left edge of the body for odd page
\pgfmathsetlengthmacro{\textlefteven}{1.0 in + \evensidemargin + \hoffset}
%% this is the left edge of the body for even page

\setlength{\TPHorizModule}{1.0 pt}
%% set the unit used by 'textpos' to be 'pt' because the length returned by 'pdf' macros are in 'pt'

\pagestyle{fancy} %% set up page header
\lhead{left head}
\chead{center head}
\rhead{page \thepage}
\cfoot{}

\begin{document}

\title{title}
\date{\today}
\author{author}

\ifthispageodd{\textblockorigin{\textleftodd}{\texttop}}{\textblockorigin{\textlefteven}{\texttop}}
%% set the origin used by 'textpos' at top left of page for even and odd pages differently
\pgfmathwidth{"note for page \thepage"}
%% store the length of wanted text into '\pgfmathresult'
\begin{textblock}{\pgfmathresult}[0, 0](0, 0)
%% '[0, 0]' sets top left corner of text block to be used as anchor point, '(0, 0)' sets the anchor point right at the origin of 'textpos'
\noindent
note for page \thepage
\end{textblock}

\ifthispageodd{\textblockorigin{\textrightodd}{\texttop}}{\textblockorigin{\textrighteven}{\texttop}}
%% set the origin used by 'textpos' at top right of page for even and odd pages differently
\pgfmathwidth{"note for page \thepage"}
\begin{textblock}{\pgfmathresult}[1, 0](0, 0)
%% '[1, 0]' sets top right corner of text block to be used as anchor point, '(0, 0)' sets the anchor point right at the origin of 'textpos'
\noindent
note for page \thepage
\end{textblock}

\maketitle

\thispagestyle{fancy}

\lipsum[6]

\newpage

\ifthispageodd{\textblockorigin{\textleftodd}{\texttop}}{\textblockorigin{\textlefteven}{\texttop}}
\pgfmathwidth{"note for page \thepage"}
\begin{textblock}{\pgfmathresult}[0, 0](0, 0)
\noindent
note for page \thepage
\end{textblock}

\ifthispageodd{\textblockorigin{\textrightodd}{\texttop}}{\textblockorigin{\textrighteven}{\texttop}}
\pgfmathwidth{"note for page \thepage"}
\begin{textblock}{\pgfmathresult}[1, 0](0, 0)
\noindent
note for page \thepage
\end{textblock}

\lipsum[2]

\newpage

\ifthispageodd{\textblockorigin{\textleftodd}{\texttop}}{\textblockorigin{\textlefteven}{\texttop}}
\pgfmathwidth{"note for page \thepage"}
\begin{textblock}{\pgfmathresult}[0, 0](0, 0)
\noindent
note for page \thepage
\end{textblock}

\ifthispageodd{\textblockorigin{\textrightodd}{\texttop}}{\textblockorigin{\textrighteven}{\texttop}}
\pgfmathwidth{"note for page \thepage"}
\begin{textblock}{\pgfmathresult}[1, 0](0, 0)
\noindent
note for page \thepage
\end{textblock}

\lipsum[3]

\newpage

\ifthispageodd{\textblockorigin{\textleftodd}{\texttop}}{\textblockorigin{\textlefteven}{\texttop}}
\pgfmathwidth{"note for page \thepage"}
\begin{textblock}{\pgfmathresult}[0, 0](0, 0)
\noindent
note for page \thepage
\end{textblock}

\ifthispageodd{\textblockorigin{\textrightodd}{\texttop}}{\textblockorigin{\textrighteven}{\texttop}}
\pgfmathwidth{"note for page \thepage"}
\begin{textblock}{\pgfmathresult}[1, 0](0, 0)
\noindent
note for page \thepage
\end{textblock}

\lipsum[4]

\end{document}

这是生产出来的

文本块

对于页面几何形状,您可以参考此图

布局

相关内容