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.

A T
  • 4,093
  • 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... –  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) –  Jun 18 '17 at 16:55
  • Please provide a compilable document, not just fragments of code –  Jun 18 '17 at 16:55
  • @ChristianHupfer https://tex.stackexchange.com/questions/375535/environment-variable-def-if-defined-else-def-default#comment929367_375535 – David Carlisle Jun 18 '17 at 16:57
  • I've tried with \IfStrEq from the xstring pacakge and \ifx. Neither worked. I've got this test-case repository, but I'm not sure how to resolve the \input macro (which isn't recognised). Trying to find what package to \usepackage for… – A T Jun 18 '17 at 16:58
  • What about \pdfstrcmp ? –  Jun 18 '17 at 17:03

1 Answers1

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.
David Carlisle
  • 757,742
  • 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 05: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 06:35
  • Hmm, well does it matter if \newgetenv[; \ifx; is in the prelude (before \begin{document})? – A T Jun 19 '17 at 06: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 06: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 06:52
  • @AT yes as written it does just what you asked and shows FOO unless you export ICT=ICT when it shows BAR – David Carlisle Jun 19 '17 at 08:03
  • It only every shows FOO for me. I should highlight my first comment: [Expectation] when ICT environment variable isn't defined: fallback to default value. – A T Jun 19 '17 at 08:04
  • @AT I suggest you fix the question as I can not reconcile what the question asks "Now I want to check if whatever \ICT evaluates to is simply what \A evaluates to; i.e.: "ICT"." with what you ask in comments. The posted answer addresses the posted question, it does not test for anything being undefined or empty. Perhaos you simply want to change the code to have \def\A{} so it tests against empty but I really can not guess. – David Carlisle Jun 19 '17 at 08:13