0

In short:

Say the following is built using pdflatex -output-directory=tmp:

\documentclass{article}
\newwrite\fileHandle
\immediate\openout\fileHandle=outfile.csv
\immediate\write\fileHandle{hello world}
\closeout\fileHandle
\begin{document}
    ...
\end{document}

Now the file outfile.csv is in the tmp directory.

How can I, using LaTeX only, access the path (considering the -output-directory) of outfile.csv?


Motivation and end goal:

We're making something along the lines of knitr, just for python.

A file, let's call it outfile.csv, is made by LaTeX using \newwrite. Now, I want LaTeX to run a command via \write18 to process this file.

The problem is that if it's run with e.g. the -output-directory flag from pdflatex (or latexmk) the path to this file is not the same as the .tex file.

Consider the following:

\documentclass{article}
% Open a write filehandle
\newwrite\fileHandle
\immediate\openout\fileHandle=outfile.csv
%^ I need the absolute path (or a path relative to this .tex file) to outfile.csv
\newcommand\writeLine[1]{
    \immediate\write\fileHandle{#1}
}
\newcommand\processCSV[1]{
  % This now creates a file called pathtooutfile
  % (Just a dummy operation. In production would be a call to a software
  % that actually processed the file).
    \immediate\write18{echo #1 > pathtooutfile}
}
\AtEndDocument{%
    % Close CSV file
    \closeout\fileHandle
    % Now, process CSV file, but I need the path!
    \def\pathToOutfile{}
    % \pathToOutfile should contain the actual path to outfile.csv.
    \processCSV{\pathToOutfile}
}
\begin{document}
    % Write some arbitrary lines
    \writeLine{comma,separated,value}
    \writeLine{more,separated,value}
    Hello!
\end{document}

Now, assume this file is run using pdflatex -output-directory=tmp/, then I want the file pathtooutfile (that is the \pathToOutfile command) to contain tmp/outfile.csv or /absolute/path/to/outfile.csv. Can this be achieved?

  • 1
    not so helpful comment, but: https://tex.stackexchange.com/questions/432428/texmaker-how-to-use-filename-as-the-output-directory-for-pdflatex#comment1083772_432428 – David Carlisle Sep 23 '18 at 12:14
  • @DavidCarlisle So you're saying that you shouldn't use the -output-directory-option? I use it by default with latexmk and haven't had any problems. – Andreas Storvik Strauman Sep 25 '18 at 18:08
  • 1
    It causes a lot of problems for maintainers of tools like latexmk to keep things working even if it seems to work Ok as a user. It's at best a cosmetic feature and it vastly complicates the entire tex tool chain, it's a shame it got added. – David Carlisle Sep 25 '18 at 18:11
  • @DavidCarlisle I can see that. But it's still there, and now I have to, as you say, develop the software around it. I wish I could access it somehow! Like pdftex could make a macro like \jobname but \outdir or something. – Andreas Storvik Strauman Sep 25 '18 at 18:12
  • if the user is calling some wrapper program latexmk or your own python script or anything else that handles the arguments, you can save the output directory (or any other command-line flags) in the environment and pick them up as needed. if you need to allow the user to call pdflatex directly on the commandline and then from within tex work out where the output is going then it's ... harder. – David Carlisle Sep 25 '18 at 18:16
  • @DavidCarlisle But can't the developers of pdflatex do something? I can't imagine it's that much work to implement some form of communication of command line args? – Andreas Storvik Strauman Sep 25 '18 at 18:19
  • pdftex is more or less frozen anyway, I thought I might see it in the Lua tex status table in luatex, but no, and to be generally accessible from latex would also need to be in xetex and ptex as well. Anything is possible but tex's famous stability has some costs...... – David Carlisle Sep 25 '18 at 18:32
  • You could do this by invoking pdflatex by something like pdflatex -output-directory=output '\def\OUTDIR{output/}\input{texfile}'. This would give you a TeX variable \OUTDIR with the information you need. A command line like this can be configured in latexmk. – John Collins Sep 27 '18 at 21:28
  • @DavidCarlisle As the latexmk maintainer, I actually like the -output-directory option a lot, and use it regularly. It didn't actually take much work to get latexmk to support it (with the main complication being about bibtex). But such things are a matter of opinion and preferred workflow. – John Collins Sep 27 '18 at 21:36
  • @JohnCollins I apologise on speaking on your behalf:-) – David Carlisle Sep 28 '18 at 06:53
  • @JohnCollins I need to know it without the user specifying it like that (just access it in LaTeX as long as it's built). Also, for the recorrd, I use the -output-directory a lot too, and in the atom package for LaTeX, it's a setting that's easily settable! – Andreas Storvik Strauman Sep 28 '18 at 12:40

0 Answers0