获得正确的标题

获得正确的标题

如何在所有页面中重复页眉?

    \documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{mwe}%%<-- only for example text, not necessary for real document

\usepackage{lastpage}%% <-- package to remember how many pages are in document (requires two runs of LaTeX)
\usepackage{fancyhdr}%% <-- very nice package for headers and footers of each page
\pagestyle{fancy}

\renewcommand\headrulewidth{0pt}
\renewcommand\footrulewidth{0pt}
%% setting the header and footers           
%% read the documentation for `fancyhdr`    
%% this isn't the only way to approach this.
\lhead{}
\chead{}
\rhead{}
\lfoot{}
%% OK, probably fancier than you need, but it gives you an idea 
%% that these things can be done according to pages             
%% This doesn't have to be an if/then/else statement.           
\cfoot{\ifnum\thepage=1\relax Copyright blah blah blah\else Something else\fi}
\rfoot{\thepage\ of \pageref{LastPage}}%% 

%% A way to store this information.  Yes, LaTeX has its own 
%% internals for these sorts of things.  I just want to show
%% alternatives.                                            
\newcommand\mytitle{THIS IS MY TITLE}
\newcommand\mysubtitle{this is my subtitle}

\begin{document}

\noindent
\begin{tikzpicture}
  \coordinate (TL) at (0,0);
  \coordinate (TR) at (\columnwidth,0);
  \node[anchor=north west,draw,inner sep=0pt] (PIC) at (TL) {\includegraphics[width=2in]{example-image-a}};
  \coordinate (BR) at (PIC.south-|TR);
  \coordinate (CR) at (PIC.east-|TR);
  \draw (TL) rectangle (BR);
  \draw (PIC.east) -- (CR);
  \path (PIC.east) -- node[midway] {\mytitle}    (TR);
  \path (PIC.east) -- node[midway] {\mysubtitle} (BR);
\end{tikzpicture}


{\centering\Large 
  \mytitle\ Again
\par\addvspace{2ex}}
%% \par triggers \centering to take effect             
%% parentheses prevent this from effecting the rest of 
%% the document.                                       

  ARMA virumque cano, Troiae qui primus ab oris
  Italiam, fato profugus, Laviniaque venit
  litora, multum ille et terris iactatus et alto
  vi superum saevae memorem Iunonis ob iram;
  multa quoque et bello passus, dum conderet urbem,
  inferretque deos Latio, genus unde Latinum,
  Albanique patres, atque altae moenia Romae.

  Musa, mihi causas memora, quo numine laeso,
  quidve dolens, regina deum tot volvere casus
  insignem pietate virum, tot adire labores
  impulerit.  Tantaene animis caelestibus irae?

  Urbs antiqua fuit, Tyrii tenuere coloni,
  Karthago, Italiam contra Tiberinaque longe
  ostia, dives opum studiisque asperrima belli;
  quam Iuno fertur terris magis omnibus unam
  posthabita coluisse Samo; hic illius arma,
  hic currus fuit; hoc regnum dea gentibus esse,
  si qua fata sinant, iam tum tenditque fovetque.
  Progeniem sed enim Troiano a sanguine duci
  audierat, Tyrias olim quae verteret arces;
  hinc populum late regem belloque superbum
  venturum excidio Libyae:  sic volvere Parcas.
  Id metuens, veterisque memor Saturnia belli,
  prima quod ad Troiam pro caris gesserat Argis---
  necdum etiam causae irarum saevique dolores
  exciderant animo:  manet alta mente repostum
  iudicium Paridis spretaeque iniuria formae,
  et genus invisum, et rapti Ganymedis honores.
  His accensa super, iactatos aequore toto
  Troas, reliquias Danaum atque immitis Achilli,
  arcebat longe Latio, multosque per annos
  errabant, acti fatis, maria omnia circum.
  Tantae molis erat Romanam condere gentem!

  Vix e conspectu Siculae telluris in altum
  vela dabant laeti, et spumas salis aere ruebant,
  cum Iuno, aeternum servans sub pectore volnus,
  haec secum:  'Mene incepto desistere victam,
  nec posse Italia Teucrorum avertere regem?
  Quippe vetor fatis.  Pallasne exurere classem
  Argivom atque ipsos potuit submergere ponto,
  unius ob noxam et furias Aiacis Oilei?

\vspace*{\fill}

\begin{center}
\includegraphics[height=3in]{example-image}
\end{center}

\pagebreak

something else

\end{document}

答案1

这可能是一个坏主意(至少就目前的大小而言),但以下将您的东西放入每个标题中:

\documentclass{article}
\usepackage[margin=1in]{geometry} % <--- This line changed
\usepackage{graphicx}
\usepackage{tikz}
\usepackage{mwe}%%<-- only for example text, not necessary for real document

\usepackage{lastpage}%% <-- package to remember how many pages are in document (requires two runs of LaTeX)
\usepackage{fancyhdr}%% <-- very nice package for headers and footers of each page
\pagestyle{fancy}

\renewcommand\headrulewidth{0pt}
\renewcommand\footrulewidth{0pt}
%% setting the header and footers           
%% read the documentation for `fancyhdr`    
%% this isn't the only way to approach this.
\lhead{}
\chead{\usebox\MyHeaderContent} % <--- This line changed
\rhead{}
\lfoot{}
%% OK, probably fancier than you need, but it gives you an idea 
%% that these things can be done according to pages             
%% This doesn't have to be an if/then/else statement.           
\cfoot{\ifnum\thepage=1\relax Copyright blah blah blah\else Something else\fi}
\rfoot{\thepage\ of \pageref{LastPage}}%% 

%% A way to store this information.  Yes, LaTeX has its own 
%% internals for these sorts of things.  I just want to show
%% alternatives.                                            
\newcommand\mytitle{THIS IS MY TITLE}
\newcommand\mysubtitle{this is my subtitle}

% This added
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newsavebox\MyHeaderContent
\savebox\MyHeaderContent
  {%
    \begin{tikzpicture}
      \coordinate (TL) at (0,0);
      \coordinate (TR) at (\textwidth,0);
      \node[anchor=north west,draw,inner sep=0pt] (PIC) at (TL) 
        {\includegraphics[width=2in]{example-image-a}};
      \coordinate (BR) at (PIC.south-|TR);
      \coordinate (CR) at (PIC.east-|TR);
      \draw (TL) rectangle (BR);
      \draw (PIC.east) -- (CR);
      \path (PIC.east) -- node[midway] {\mytitle}    (TR);
      \path (PIC.east) -- node[midway] {\mysubtitle} (BR);
    \end{tikzpicture}%
  }
\geometry
  {%
    margin=1in
    ,includeheadfoot
    ,headheight=\dimexpr\ht\MyHeaderContent+1.1\dp\MyHeaderContent\relax
  }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

{\centering\Large 
  \mytitle\ Again
\par\addvspace{2ex}}
%% \par triggers \centering to take effect             
%% parentheses prevent this from effecting the rest of 
%% the document.                                       

  ARMA virumque cano, Troiae qui primus ab oris
  Italiam, fato profugus, Laviniaque venit
  litora, multum ille et terris iactatus et alto
  vi superum saevae memorem Iunonis ob iram;
  multa quoque et bello passus, dum conderet urbem,
  inferretque deos Latio, genus unde Latinum,
  Albanique patres, atque altae moenia Romae.

  Musa, mihi causas memora, quo numine laeso,
  quidve dolens, regina deum tot volvere casus
  insignem pietate virum, tot adire labores
  impulerit.  Tantaene animis caelestibus irae?

  Urbs antiqua fuit, Tyrii tenuere coloni,
  Karthago, Italiam contra Tiberinaque longe
  ostia, dives opum studiisque asperrima belli;
  quam Iuno fertur terris magis omnibus unam
  posthabita coluisse Samo; hic illius arma,
  hic currus fuit; hoc regnum dea gentibus esse,
  si qua fata sinant, iam tum tenditque fovetque.
  Progeniem sed enim Troiano a sanguine duci
  audierat, Tyrias olim quae verteret arces;
  hinc populum late regem belloque superbum
  venturum excidio Libyae:  sic volvere Parcas.
  Id metuens, veterisque memor Saturnia belli,
  prima quod ad Troiam pro caris gesserat Argis---
  necdum etiam causae irarum saevique dolores
  exciderant animo:  manet alta mente repostum
  iudicium Paridis spretaeque iniuria formae,
  et genus invisum, et rapti Ganymedis honores.
  His accensa super, iactatos aequore toto
  Troas, reliquias Danaum atque immitis Achilli,
  arcebat longe Latio, multosque per annos
  errabant, acti fatis, maria omnia circum.
  Tantae molis erat Romanam condere gentem!

  Vix e conspectu Siculae telluris in altum
  vela dabant laeti, et spumas salis aere ruebant,
  cum Iuno, aeternum servans sub pectore volnus,
  haec secum:  'Mene incepto desistere victam,
  nec posse Italia Teucrorum avertere regem?
  Quippe vetor fatis.  Pallasne exurere classem
  Argivom atque ipsos potuit submergere ponto,
  unius ob noxam et furias Aiacis Oilei?

\vspace*{\fill}

\begin{center}
\includegraphics[height=3in]{example-image}
\end{center}

\pagebreak

something else

\end{document}

我标记了我更改的行% <--- This line changed、我添加的块% This added和一行%字符。

结果(仅第一页):

在此处输入图片描述

根据评论中@esdd 的建议进行了编辑

答案2

以下是我处理此问题的方法。

在此处输入图片描述

您可以调整各种设置geometry以使其更符合您的喜好。我还使用了选项includeheadgeometry包实际计算页面上文本的高度。

获得正确的标题

您可以通过向命令添加内容在每个页面上获得相同的标题:

\lhead{}
\chead{}
\rhead{}

所以如果我写类似的东西

\lhead{Hello world}

然后该短语Hello world将出现在每一页的左上角(直到我更改它)。

您想将图像放在页面顶部。有几种方法可以做到这一点。我更喜欢保存静态内容,这些内容将在预处理的框中经常重复使用。所以这就是我在这里所做的:

\newcommand\mytitle{THIS IS MY TITLE}
\newcommand\mysubtitle{this is my subtitle}
\usepackage{tikz}
\newsavebox\aebox
\begin{lrbox}\aebox
  \begin{minipage}[b]{\dimexpr8.5in-2in}
    \noindent
    \begin{tikzpicture}[baseline=(PIC.south)]
      \coordinate (TL) at (0,0);
      \coordinate (TR) at (\columnwidth,0);
      \node[anchor=north west,draw,inner sep=0pt] (PIC) at (TL) {\includegraphics[width=2in]{example-image-a}};
      \coordinate (BR) at (PIC.south-|TR);
      \coordinate (CR) at (PIC.east-|TR);
      \draw (TL) rectangle (BR);
      \draw (PIC.east) -- (CR);
      \path (PIC.east) -- node[midway] {\mytitle}    (TR);
      \path (PIC.east) -- node[midway] {\mysubtitle} (BR);
    \end{tikzpicture}
  \end{minipage}
\end{lrbox}

这出现在我的前言在我做其他任何事情之前。我假设您使用的是 8.5x11 纸张。如果不是,则将上述内容更改8.5in为您使用的纸张的正确宽度。我还假设您使用1in两边的边距。因此

\dimexpr8.5in-2in

处理我的框内容所需的任何包和命令都需要在环境之前加载lrbox。如果您是 LaTeX 新手,lrboxes 可能比您目前需要了解的要多。只需知道此环境用于将其内容保存在一个您可以重复使用的框中,而无需重新处理代码(如果此内容位于每页的顶部,则可以节省大量时间)。

lrbox您应该熟悉的重点是minipage环境。它对于处理您想要以不同于页面一般内容的方式呈现的段落和内容非常方便。语法是

\begin{minipage}[<position>]{<width>}
   ...CONTENT...
\end{minipage}

<position>指的是内容应如何相对于周围文本对齐。由于这是在标题中,因此我希望在其基线上对齐。

基线TikZ 图片也需要设置。这就是我在这里做的:

\begin{tikzpicture}[baseline=(<node>)]

这告诉 TikZ 我希望图片的底部与命名的位于相同的垂直位置<node>

使用盒子(此处\aebox)的好处是,我可以提取其尺寸。它有三个:

  • 高度
  • 深度
  • 宽度

我通过这封信来说明这一点g

在此处输入图片描述

我构建了页眉,使其没有深度。但现在我可以在确定页面几何尺寸时参考其高度。

\usepackage[includehead,
            margin=1in,
            head=\dimexpr\ht\aebox+4pt]{geometry}

+4pt是为了在标题周围提供一个小小的缓冲区。

然后使用该fancyhdr包我可以将其设置为每个页面的标题

\chead{\usebox{\aebox}}

相关内容