5

How to add the document's filename at the top left corner of each page in the following minimum working example:

\documentclass{article}
\begin{document}
    % \markboth{\jobname}{\jobname}
    Hello!
    \clearpage
    This is the second page.
\end{document}

I have tried \markboth{\jobname}{\jobname} just after \begin{document} but in vain.

2 Answers 2

8

Your code is fine. You just need to switch from the default page style of the article document class (plain) to a page style that allows for a non-empty header line. LaTeX provides the myheadings page styles for your purpose. (The page style headings might work as well -- but only if the document doesn't contain any section-level headers.)

\documentclass{article}
\pagestyle{myheadings}
\begin{document}
    \markboth{\jobname}{\jobname}
    Hello!
    \clearpage
    This is the second page.
\end{document}
2
  • 1
    If I have a section heading, then the \pagestyle{headings} gave me the section heading instead of the filename. In such case, the \pagestyle{myheadings} gave me the filename on each page.
    – Muaz
    May 7, 2018 at 4:28
  • 1
    @MMKhan - Many thanks for this feedback. I will update my answer accordingly.
    – Mico
    May 7, 2018 at 5:04
5

For example with the fancyhdr package:

\documentclass{article}

\usepackage{fancyhdr}
\pagestyle{fancy}

\begin{document}
    \markboth{}{\jobname}
    Hello!
    \clearpage
    This is the second page.
\end{document}
1
  • fancyhdr is adding this horizontal line which I do not want. The \pagestyle{headings} is fine without using the fancyhdr package. Thanks.
    – Muaz
    May 4, 2018 at 10:18

You must log in to answer this question.

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