3

I wrote a simple makefile to compile my latex code.

I have some variables in that makefile and I want to pass them to the latex code, but I don't know how to do that.

Example 1:

in makefile:

# set Type to A or B
Type = B

in latex code:

\ifdefined\B
%%%%%%%%%%%%%%%Some LaTeX Code%%%%%%%%%%%%%%%
\else
\ifdefined\A
%%%%%%%%%%%%%%%Some LaTeX Code%%%%%%%%%%%%%%%
\fi
\fi

Example 2:

in makefile:

Version = J

in LaTeX code:

Some random text \Version

Here is the line in the makefile that I use for compilation:

latexmk -lualatex -output-directory=$(OUTDIR) --jobname=$(REPORT_PDF) $(REPORT)

Any help?

dexteritas
  • 9,161
Nour
  • 347

2 Answers2

4

As you are using luatex you can easily access the environment

\documentclass{article}

\begin{document}

\directlua{tex.write(os.getenv("FOO"))}

\end{document}

will print 7 given

 FOO=7 lualatex file

so you can then test using Lua or TeX condtional code/

David Carlisle
  • 757,742
  • to pass the make variable to the environment either add it to the shell command as here or see https://stackoverflow.com/questions/23843106/how-to-set-child-process-environment-variable-in-makefile – David Carlisle Nov 15 '21 at 11:25
  • Nice solution, one caveat is that if the environment variable changes, latexmk will (I believe) not notice and so if that is the only change, it will not recompile even though the expected output would be different. – Vincent Beffara Nov 15 '21 at 12:16
  • @VincentBeffara I guess you have a make clean target that would wake latexmk up :-) – David Carlisle Nov 15 '21 at 12:21
3

You can always, in your Makefile, create an auxiliary file that LaTeX will read: namely, in the Makefile,

echo "\\def\\foo{$WHATEVER}" > foo.inc

and in your .tex file, just \input{foo.inc}.

Alternative option, you can do something a bit less brittle using the catchfile package that sets a LaTeX macro to the contents of a file: namely, in the Makefile,

echo $WHATEVER > foo.var

and in your .tex file,

\usepackage{catchfile}
\CatchFileDef{\foo}{}{foo.var}

(or, use \file_get:nnN if you prefer).

In both cases, latexmk will notice the changes to these auxiliary files and trigger recompilation when they change.

Vincent Beffara
  • 246
  • 1
  • 11