1

How do I compare two macros that are one-expansion away from being strings?

\def\A{ICT}
\newgetenv[\ICT]{\A}
% `\newgetenv` from https://tex.stackexchange.com/a/184924

Now I want to check if whatever \ICT evaluates to is simply what \A evaluates to; i.e.: "ICT".

Attempts (one line at a time; others commented):

\ifdefequal{\expandafter\ICT}{\expandafter\A}{\def\ICT{FOO}}{\def\ICT{BAR}}
\ifdefequal{\ICT}{\expandafter\A}{\def\ICT{FOO}}{\def\ICT{BAR}}
\ifdefequal{\expandafter\ICT}{\A}{\def\ICT{FOO}}{\def\ICT{BAR}}

I've also tried the same 3 possibilities with \ifcsequal and \ifdefstring. Also tried giving up on the \A and just using: ICT verbatim.

But they all define an \ICT with a value of BAR, no matter what I set my $ICT system environment variable to.

  • Just a guess, have you checked the xstring package? – Dr. Manuel Kuehner Jun 18 '17 at 16:50
  • It's hard to guess what you expect the \expandafter to do in those situations. They will just expand into the internal definition of ifdefequal don't you just want \ifx\A\ICT yes \else no\fi ? – David Carlisle Jun 18 '17 at 16:52
  • \newgetenv from one of my answers apparently... – user31729 Jun 18 '17 at 16:53
  • @ChristianHupfer yes there is a link to your answer in the comment in the code (not the most helpful place to have it but it is there) – David Carlisle Jun 18 '17 at 16:54
  • @DavidCarlisle: Yes, I saw that -- didn't remember the answer and I am not sure it's helpful at all (I mean the answer) – user31729 Jun 18 '17 at 16:55
1

Making a usable test file...

\documentclass{book}%
\usepackage{etoolbox}%
\usepackage{xstring}%
\usepackage{catchfile}%

\newcommand{\getenv}[2][]{%
  \CatchFileEdef{\temp}{"|kpsewhich --var-value #2"}{}%
  \if\relax\detokenize{#1}\relax\temp\else\let#1\temp\fi}
%\getenv[\INCLUDE]{\string INCLUDE}

\def\newtemp{}%
\newcommand{\newgetenv}[2][]{%
  \CatchFileEdef{\temp}{"|kpsewhich --var-value #2"}{}%
  \StrGobbleRight{\temp}{1}[\newtemp]%  Delete the trailing whitespace character
  \if\relax\detokenize{#1}\relax\temp\else\edef#1{\newtemp}\fi%
}%

\begin{document}

\def\A{ICT}
\newgetenv[\ICT]{\A}

\ifx\ICT\A
\def\ICT{BAR}
\else
\def\ICT{FOO}
\fi

\show\ICT

\end{document}

by default defines \ICT as FOO

 pdflatex pp024

produces

> \ICT=macro:
->FOO.
l.32 \show\ICT

But if the environment variable ICT has value ICT (which appears to be the test that you were trying to implement, although you do not say) then \ICT is defined as BAR.

 ICT=ICT pdflatex pp024

produces

> \ICT=macro:
->BAR.

A much simpler definition is

\documentclass{book}

\begin{document}

\def\A{ICT}

\ifx\ICT\A
\def\ICT{BAR}
\else
\def\ICT{FOO}
\fi

\show\ICT

\end{document}

where

pdflatex \\def\\ICT{$ICT}\\input pp024

produces

> \ICT=macro:
->FOO.

unless ICT=ICT when you get

 ICT='ICT' ;pdflatex \\def\\ICT{$ICT}\\input pp024

which produces

> \ICT=macro:
->BAR.
  • Actually I was testing if ICT environment variable was defined. When it's not defined, for some reason it gets the verbatim ICT text. I'm using latexmk to build my LaTeX project, within a regular Makefile. Your code did not work in this setup. – A T Jun 19 '17 at 5:25
  • @AT The code is pure tex code so will work the same way whether tex is called from the command line or from latexmk or a makefile. the form setting the environment variable on the same line is bash syntax but there is no requirement to set the variable in that way, just set it however you wish. – David Carlisle Jun 19 '17 at 6:35
  • Hmm, well does it matter if \newgetenv[; \ifx; is in the prelude (before \begin{document})? – A T Jun 19 '17 at 6:40
  • @AT probably not, but you've posted two questions on this now and still not shown a reproducible example of what you are doing or what error you get. – David Carlisle Jun 19 '17 at 6:42
  • Using your exact code example, and it doesn't work. pdflatex example0.tex; > \ICT=macro: ->FOO. and running export ICT=foo beforehand still gives same output. – A T Jun 19 '17 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.