# How to get file name from inside a latex file?

In the context of biblatex its possible to bibstyle=reading, this will produce bibliography including abstract and annotation fields.

The annotation might come from a file named like bibannotation-TheBook2019.tex where TheBook2019 is the bib file entry key.

@book{TheBook2019,
author     = {The Author},
title      = {The Book Title}
}


The annotation file bibannotation-TheBook2019.tex can contain latex formated text. One might then \input{bibannotation-TheBook2019.tex} somewhere in a document. And so might be appropriate to include a citation in the bibannotation-TheBook2019.tex. Is this case it willl be self citation, I mean that file will refer to the respective entry key:

\cite{TheBook2019}


Since this information is in the file name. Can I automate it some way? So that I can just type something like:

\cite{\SelfCite}


And it will be converted to \cite{TheBook2019} grabbing the entry key from file name.

You can do this with a couple of helper functions.

\KcFnMiinput saves the name of the key using \filenametokey then inputs the file.

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
@book{TheBook2019,
author     = {The Author},
title      = {The Book Title}
}
\end{filecontents}
\begin{filecontents}{annotation-TheBook2019.tex}
This file contains an annotation for \verb|TheBook2019|. It's also possible to
cite it: \cite{\selfcitekey}.
\end{filecontents}
\usepackage{biblatex}

• Please, could you update answer so that I can also refer to bib files in another folder, like \input{bib/bibannotation-TheBook2019.tex}? – KcFnMi May 7 '19 at 4:49
• Can newcommand be used instead of def ? Considering tex.stackexchange.com/questions/655/…. – KcFnMi May 7 '19 at 4:55
• @KcFnMi It's not possible to use \newcommand for \filenametokey. It (essentially) uses the technique explained in tex.stackexchange.com/a/662/87678. You can modify the template however you want. Just use \def\filenametokey bib/annotation-#1.tex{#1} and then \KcFnMiinput{bib/annotation-TheBook2019.tex}. (This isn't so easy to do in the MWE though, since filecontents can't output to a subdirectory without --shell-escape to create the subdirectory first if it doesn't exist.) – David Purton May 7 '19 at 5:09