3

I am trying to make a specific header for the first page while the header for the rest of the pages will be the same but different from the first page. The header for the first and the second pages is shown below (only top portion of each page is shown), where the header is basically a table. My first question is how I can put a \href inside this table. I actually followed the instruction that was provided here but the compiler does not allow me to use \href. The other question is how I can create the second header different from the first page.

enter image description here

Here is the code I used.

\documentclass{article}
\usepackage[margin=1in,headheight=5\baselineskip,headsep=1\baselineskip,includehead]{geometry}
\usepackage{tabularx}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{hyperref}

\newlength{\headerwidth}
\setlength{\headerwidth}{\textwidth}
\newsavebox{\myheader}
\begin{lrbox}{\myheader}%
    \begin{minipage}[b]{\headerwidth}
    \renewcommand{\arraystretch}{1}%
    \begin{tabularx}{\headerwidth}{p{0.75\linewidth}p{0.25\linewidth}}
        {\bf FirstName LastName} & \\
        Address1 & Phone +1 (123) 456 7890 \\
        Address2 & Fax +1 (123) 456 7890\\
        Address3 & \href{mailto:[email protected]}{[email protected]}\\
    \end{tabularx}
    \end{minipage}
\end{lrbox}

\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\lhead{}
\chead{\usebox{\myheader}}
\rhead{}
\lfoot{}
\cfoot{}
\rfoot{}

\usepackage{lipsum}
\begin{document}
\lipsum[1-3]
\end{document}
2
  • As always: Please provide an MWE which results in your posted picture or something close. Nobody wants to type all of this example in order to insert one \href. Thanks.
    – LaRiFaRi
    Jul 30, 2014 at 7:31
  • Can you provide a minimal working example (MWE)? Without one it's really hard to help.
    – Johannes_B
    Jul 30, 2014 at 7:31

1 Answer 1

4

\href should be used only after \begin{document}. Surround the lrbox with \AtBeginDocument{...}:

\AtBeginDocument{%
  \begin{lrbox}{\myheader}%
    \begin{minipage}[b]{\headerwidth}
    \renewcommand{\arraystretch}{1}%
    \begin{tabularx}{\textwidth}{@{}Xl@{}}
        \textbf{FirstName LastName} & \\
        Address1 & Phone +1 (123) 456 7890 \\
        Address2 & Fax +1 (123) 456 7890\\
        Address3 & \href{mailto:[email protected]}{[email protected]}
    \end{tabularx}%
    \end{minipage}%
  \end{lrbox}%
}

Note that I have changed the column Definition to @{}Xl@{} to avoid an overfull hbox. And I have removed the \\ behind \href.

enter image description here

Alternatively you can use \newcommand\myheader{...} instead of saving the table in a lrbox.

\documentclass{article}
\usepackage[margin=1in,headheight=5\baselineskip,headsep=1\baselineskip,includehead]{geometry}
\usepackage{tabularx}
\usepackage{fancyhdr}
\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
\fancyhf{}
\chead{\myheader}
\newcommand\myheader{%
    \begin{minipage}[b]{\textwidth}
    \renewcommand{\arraystretch}{1}%
    \begin{tabularx}{\linewidth}{@{}Xl@{}}
        \textbf{FirstName LastName} & \\
        Address1 & Phone +1 (123) 456 7890 \\
        Address2 & Fax +1 (123) 456 7890\\
        Address3 & \href{mailto:[email protected]}{[email protected]}
    \end{tabularx}%
    \end{minipage}%
}

\usepackage{lipsum}

\usepackage{hyperref}% normally the last package

\begin{document}
\lipsum[1-3]
\end{document}

One possibility to get an other header on all pages except the first page is creating a new pagestyle firstpage and using \thispagestyle{firstpage}:

\documentclass{article}
\usepackage[margin=1in,headheight=5\baselineskip,headsep=1\baselineskip,includehead]{geometry}
\usepackage{tabularx}
\usepackage{fancyhdr}
\pagestyle{fancy}
\chead{other pages}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
%
\fancypagestyle{firstpage}{% new pagestyle
  \fancyhf{}
  \chead{\myheader}}
  \newcommand\myheader{%
      \begin{minipage}[b]{\textwidth}
      \renewcommand{\arraystretch}{1}%
      \begin{tabularx}{\linewidth}{@{}Xl@{}}
          \textbf{FirstName LastName} & \\
          Address1 & Phone +1 (123) 456 7890 \\
          Address2 & Fax +1 (123) 456 7890\\
          Address3 & \href{mailto:[email protected]}{[email protected]}
      \end{tabularx}%
      \end{minipage}%
}
%
\usepackage{lipsum}

\usepackage{hyperref}% normally the last package
\begin{document}
\thispagestyle{firstpage}
\lipsum[1-10]
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .