# \inputting contents of a file into a hyperref's pdfkeywords

I use git to manage the development of my LaTeX documents, and I have a makefile I use to build a PDF from the LaTeX source. For the sake of provenance, I would like to embed the most recent git commit hash into the metadata of the resulting PDF. In that way I can send out PDFs and have a fighting chance of figuring out whence they came.

My approach is to have some code in the makefile which stores the current commit hash in a temporary file, then read the contents of that file into the pdfkeywords field of the hyperref package using the \input directive like so:

\documentclass[letterpaper,12pt]{article}

\hypersetup{
pdftitle={A method for embedding git hash into PDF metadata},
pdfauthor={Joshua R. Smith},
pdfsubject={},
pdfkeywords={\input{contains_git_hash.txt}},
pdfcreator={pdfTeX}
}

\begin{document}

\maketitle

\section{Git should play well with \LaTeX}
But does it?

\end{document}


Unfortunately, the above code simply embeds the string "contains_git_hash.txt" in the "Keyword" metadata field of the resulting PDF instead of the contents of the file.

What am I missing?

-
Welcome to TeX SE! Why not use gitinfo? – cfr Apr 24 '14 at 22:52
Thanks for the welcome! With the caveat that I've never used gitinfo, it looks like I would have to add a post-commit or post-checkout hook to all of my LaTeX git repos. Adding such hooks seems like overkill to me; I really want to restrict the scope of the solution to the LaTeX file and makefile without having to modify the git repo. Heiko's solution meets that requirement. – joshua.r.smith Apr 27 '14 at 16:20

You can use package catchfile to store the contents of a file in a macro:

\documentclass[letterpaper,12pt]{article}

\usepackage{catchfile}
\usepackage{trimspaces}
\CatchFileDef{\GitHash}{contains_git_hash.txt}{}
\makeatletter
\trim@spaces@in\GitHash
\makeatother

\hypersetup{
pdftitle={A method for embedding git hash into PDF metadata},
pdfauthor={Joshua R. Smith},
pdfsubject={},
pdfkeywords={\GitHash},
pdfcreator={pdfTeX}
}

\begin{document}

\title{...}
\maketitle

\section{Git should play well with \LaTeX}
But does it?

\end{document}


Remarks:

• Package trimspaces is used to remove the last space. If the file does only contain one line, the final space can also be removed by \endlinechar=-1:

\CatchFileDef{\GitHash}{contains_git_hash.txt}{\endlinechar=-1\relax}

-
Can you explain why the input command doesn't work? Does it have to do with the order in which LaTeX executes various statements? – joshua.r.smith Apr 25 '14 at 12:34
@joshua.r.smith: LaTeX's \input is not expandable (file lookup, handling non-existent files, ...). And the expandable original \input primitive, saved as \@@input in LaTeX would cause the error ! File ended while scanning definition of \@pdfkeywords. – Heiko Oberdiek Apr 25 '14 at 13:43
Thanks for the answer and the prompt response! – joshua.r.smith Apr 27 '14 at 16:27