Writing TeX file name at the top of the processed document for article class

Consider the following MWE.

\documentclass{article}
\usepackage{textpos}
\newcommand{\insertname}{%
\begin{textblock}{2}(5,-0.5)
\bfseries{\jobname.tex}
\end{textblock}
}
\begin{document}
\insertname
Some text.
\end{document}


This places the name of the LaTeX file at the top of the output PDF. However, I'd like this handled automatically, using xpatch, if possible. Then I won't need to manually insert the macro into the body, but will have this handled in the preamble. I've tried using \xpretocmd with \maketitle and \titlepage as arguments in turn, but it does not work. Any suggestions how I can do this?

\documentclass{article}
\usepackage{textpos}
\usepackage{xpatch}
\newcommand{\insertname}{%
\begin{textblock}{2}(5,-0.5)
\bfseries{\jobname.tex}
\end{textblock}
}
\xpretocmd{\maketitle}{\insertname}{}{}
%\xpretocmd{\titlepage}{\insertname}{}{}
\begin{document}
%\insertname
Some text.
\end{document}


The answer by tohecz looks good, but doesn't work with a simple letter example.

\documentclass[11pt]{letter}
\signature{Some person}

\usepackage{fancyhdr}
\fancyhf{}
\pagestyle{fancy}

\begin{document}

Some stuff

\closing{Thanking you}
\end{letter}
\end{document}


Something like this

\documentclass[a4paper]{memoir}
% picture loaded to be able to use 'units in \put
\usepackage{eso-pic,picture}
% change reference point to page upper left
\AtPageUpperLeft{
\put(1cm,-1cm){\textbf{\jobname}}
}
}
% this is just to have some contents
\usepackage{lipsum}
\begin{document}
\lipsum
\end{document}


\AddToShipoutPictureBG* just added to the next shipped out page. Remove the * and it is added to all pages.

Centered version

\documentclass[a4paper]{memoir}
% picture loaded to be able to use 'units in \put
\usepackage{eso-pic,picture,calc}
% change reference point to page upper left
\AtPageUpperLeft{
\put(\oddsidemargin+1in+0.5\textwidth,-1cm){%
\makebox[0pt][c]{\textbf{\jobname}}}
}
}
% this is just to have some contents
\usepackage{lipsum}
\begin{document}
\lipsum
\end{document}

• Can you suggest some automated way to center the text between the left and right margins? I've switched to em, but the position still seems to be moving about. – Faheem Mitha Jan 24 '14 at 16:05
• See the update. BTW em is a unit of measure, how should that center anything? – daleif Jan 24 '14 at 16:38
• Thanks. I thought em is measured relative to the page. so a x-coordinate measured in em should be invariant across different documents, but that does not appear to be the case. – Faheem Mitha Jan 24 '14 at 16:51
• EM is a dynamic unit of measure, it corresponds to the current font size, often roughly the width of an M. – daleif Jan 24 '14 at 19:07

You need to use \maketitle if you want that \insertname is executed.

However the simplistic solution of doing \AtBeginDocument{\insertname} will not work if \maketitle is used in the document, because \maketitle does \newpage.

A more complex workaround is needed if you plan not using \maketitle in your document. If you plan to use it, then \@maketitle is the macro to be patched:

\makeatletter
\xpatchcmd{\@maketitle}{\newpage}{\newpage\insertname}{}{\ddt}
\makeatother


so \insertname is executed after the \newpage.

If you want it independent of the presence of \maketitle, the only reliable way seems to be with a suitable page style.

\documentclass{article}
\usepackage{xpatch}
\makeatletter
\def\ps@faheem{%
\let\@mkboth\@gobbletwo
\def\@oddfoot{\reset@font\hfil\thepage\hfil}%
}
\AtBeginDocument{\thispagestyle{faheem}}
\xpatchcmd{\maketitle}{plain}{faheem}{}{}
\makeatother

\begin{document}
\title{A}\author{B}
\maketitle
Some text.
\end{document}


If you comment \maketitle you'll get the same header.

For completeness, here's a different strategy with atbegshi:

\documentclass{article}
\usepackage{atbegshi,picture}
\newcommand{\insertname}{%
\put(\dimexpr\oddsidemargin+1in+\textwidth,-1cm){%
\makebox[0pt][r]{\textbf{\jobname.tex}}%
}%
\gdef\insertname{}%
}
\AtBeginShipout{%
\AtBeginShipoutUpperLeft{\insertname}%
}

\begin{document}
\title{A}\author{B}
\maketitle
\end{document}


Modify the placement parameters; with this setting, the filename is placed with its right edge flush with the right margin, the baseline at 1cm from the top of the paper.

• If it just has to go in the very first page I'd just use eso-pic and \AtBeginDocument – daleif Jan 24 '14 at 10:55
• @daleif I want it at the top of the first page. Can you give more details of what you have in mind? – Faheem Mitha Jan 24 '14 at 11:16

Do I miss something or you can simply use the standard headers as I often do?

\usepackage{fancyhdr}
\fancyhf{}
\pagestyle{fancy}


This inserts the \jobname and the date of compilation into the header of every page. If you already have something in your header and you want to put this a bit higher, you can use the following construct:

\usepackage{fancyhdr}
\usepackage{mathtools}
blabla%

If you don't want to load mathtools, you can simulate \llap{abc} and \rlap{abc} by \hbox to 0pt{\hss abc} and \hbox to 0pt{abc\hss}