0

Many languages provide functions to obtain the absolute path of a file. I wonder if Tex/LaTex has also such macros.

In the following example, given a file name, test if this file really exists. Then, if yes, output its absolute path(including file name) from root like /home/foo/bar/myfile or /c/Program Files/foo/bar/myfile.tex.

Example:

\documentclass{article}

\ExplSyntaxOn \NewDocumentCommand\getabsolutepath{m}{ \file_if_exist:nTF {#1} { % output the absolute path of the file name #1 } {do some other things} } \ExplSyntaxOff

\begin{document} % Given that the current path names "cp". A file named "a.tex" locates in its subdirectory "sub". % The typesets of the following invokations of \getabsolutepath should be absolutely the same when file "a" really exists because they refer to the same file. \getabsolutepath{sub/a} \getabsolutepath{./sub/a} \getabsolutepath{./sub/a.tex} \getabsolutepath{../cp/sub/a} \end{document}

lyl
  • 2,727

1 Answers1

1

You can use kpsewhich although for files found relative to the working directory, you may want to have $PWD rather than . in the path so the full path is reported. This command is allowed without --shell-escape in the default configuration of texlive (and I think also miktex)

enter image description here

\documentclass{article}

\def\getabsolutepath#1{\input{|kpsewhich "#1"}}

\begin{document}

\getabsolutepath{article.cls}

\getabsolutepath{plain.tex}

\getabsolutepath{\jobname}

\end{document}

called by:

TEXINPUTS=$PWD: pdflatex  cc434
user202729
  • 7,143
David Carlisle
  • 757,742