I have a LaTeX document which exists in two versions, a short version and the long version. The long version has an additional chapter inserted in the middle of the document. What I want to do, is if I have an environment variable set, include the additional file at the relevant point. So I am doing this (in pdflatex):
\documentclass{book}
\usepackage{etoolbox}
\usepackage{catchfile}
\newcommand{\getenv}[2][]{%
\CatchFileEdef{\temp}{"|kpsewhich --var-value #2"}{}%
\if\relax\detokenize{#1}\relax\temp\else\let#1\temp\fi}
\getenv[\INCLUDE]{INCLUDE}
\begin{document}
First paragraph of first file.
Value of the INCLUDE envvar is as follows: \INCLUDE
\ifstrequal{\INCLUDE}{yes}{\input{file2}}{}
Last paragraph of first file.
\end{document}
I know it can read the environment variable correctly, so I can see the problem is with the conditional, but not sure exactly what I am doing wrong. When I run this with INCLUDE=yes, the value yes is printed, but file2's contents does not appear.
(This is like this question, but that question does not address environment variables. I got my environment variable code from this answer, but since I can print the environment variable correctly, I don't think that is the problem.)
Best Answer
Edit
Improved version
There are two basically two problems:
\ifstrequal
does not expand its argumentsyes
will becomeyes' '
, so that the test fails. I could not figure out the reason up to now.By usage of the
xstring
package command\StrGobbleRight
and\ifdefstring
frometoolbox
package, it is possible to cope around, hopefully, that it is just one character at the end to be deleted. See the effect as diagnostics in the top of the output document, when\temp
and\newtemp
macros output -- if the whitespace is removed, the content of the environment variable and the following text should be glued together.I redefined the
getenv
command to\newgetenv
and used an expanded\edef\temp
to make the\ifdefstring
command work with the literalyes
value.The file
file2.tex
just contains the lineAnother version (Thanks to the hint made by H. Oberdiek)
The trailing whitespace(
\endlinechar
) can be omitted by using\endlinechar=-1\relax
as 3rd argument to\CatchFileEdef
command. This simplifies the handling of the environment variable macro and the usage ofxstring
package can be dropped.I did not update the screenshot, nothing essential has changed in the output.