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} 
8
  • 1
    tex.stackexchange.com/questions/42417/full-path-of-current-file Commented Jul 15, 2022 at 7:30
  • Thank you for this info. Anyway, I don't think this meets my requirement. Those solutions needs working with \input ...and so on. What I want is just giving a file name and obtain its absolute path from root if the file really exists. Commented Jul 15, 2022 at 8:02
  • Take a look at \CurrentFilePath Commented Jul 15, 2022 at 8:21
  • Would you show me an example? Commented Jul 15, 2022 at 8:32
  • @Ingmar \CurrentFilePath doesn't give the absolute path. You need the currfile package (and you must use the --recorder option) to get the full absolute path of a file. Commented Jul 15, 2022 at 9:37

1 Answer 1

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 
14
  • Side note, use @@input for an expandable version, I think. With some additional complications of everyeof etc. Commented Jul 15, 2022 at 13:28
  • @user202729 hmm -c appears not to work in restricted mode , I'd better edit Commented Jul 15, 2022 at 13:32
  • @David Carlisle The above code failed to pass complilation in my computer (win7+miktex). showed (e:\MyOffice\Emacs_Related\Miktex\texmfs\install\tex/latex/base\ts1cmr.fd) ( ! Missing $ inserted. <inserted text> $ l.2 e:/MyOffice/Emacs_ Related/Miktex/texmfs/install/tex/latex/base/article.cls ) Commented Jul 15, 2022 at 13:33
  • @lyl ah you need a \detokenize or locally set catcode of _ to 12 if you have fancy characters like that Commented Jul 15, 2022 at 13:36
  • 1
    texdoc texosquery and texdoc kpsewhich have more information @lyl Commented Jul 15, 2022 at 13:42

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.