3

Based on my previous question, consider the following files:

test.cls

\ProvidesClass{test}
\ExplSyntaxOn
\keys_define:nn { test }{
    git .code = {
        \sys_get_shell:nnNTF {git rev-parse HEAD} {} \testgit {} {}
    }
}
\sys_get_shell:nnNTF {git rev-parse HEAD} {} \myvar {} {}
\ExplSyntaxOff
\ProcessKeyOptions[test]
\LoadClass{article}

test.tex

\documentclass[git]{test}

\begin{document}

\ExpandArgs{x}\IfBlankTF{\myvar}{}{a}
\ExpandArgs{x}\IfBlankTF{\testgit}{}{b}

\end{document}

As in the above mentioned question, a is not printed, but weirdly b is. What does \keys_define:nn do different?

5
  • it has nothing to do with the key. The one call is in the \ExplSyntax-context the other outside and so \endlinechar is different. Commented Jun 11 at 21:07
  • @UlrikeFischer Could you elaborate? This is above my paygrade! Commented Jun 11 at 21:09
  • Document Class: test (|gitrev-parseHEADsh: line 1: gitrev-parseHEAD: command not found ) (|gitrev-parseHEADsh: line 1: gitrev-parseHEAD: command not found you need e.g. git ~ rev-parse ~ HEAD. so i don't know how you get a difference between the two cases? Commented Jun 11 at 21:45
  • @cfr I'm not really sure, but \testgit is correctly set even with a .git directory. I think the reason is that, as egreg mentions in the answer or Ulrike mentions in the above comment, \testgit is not set in expl3 syntax. Commented Jun 11 at 21:52
  • Nitpicking: Inside \keys_define:nn the key property is named .code:n (.code is from the 2e layer, though internally they're the same). Commented Jun 12 at 6:07

1 Answer 1

4

It helps doing \show\myvar and \show\testgit. The former would issue

> \myvar=macro:
->.

while the latter would issue

> \testgit=macro:
->\par .

so you see that \testgit is not empty.

By the way, you also get

|gitrev-parseHEADsh: gitrev-parseHEAD: command not found

and you need

\sys_get_shell:nnNTF {git~rev-parse~HEAD}

However the problem is that when you set \myvar you're under \ExplSyntaxOn, but not when you set \testgit. In the latter case, the system returns nothing, which generates an empty line. The \input| mechanism that's exploited by \sys_get_shell:nnNTF actually generates a pseudofile and TeX always appends another EOL at the end. So you get an empty line according to TeX tokenization rules and this is tokenized as \par.

Solution:

\ProvidesClass{test}
\ExplSyntaxOn
\keys_define:nn { test }{
    git .code = {
        \sys_get_shell:nnNTF {git~rev-parse~HEAD} {\endlinechar=-1~} \testgit {} {}
    }
}
\sys_get_shell:nnNTF {git~rev-parse~HEAD} {} \myvar {} {}
\ExplSyntaxOff
\ProcessKeyOptions[test]
\LoadClass{article}

New test.tex:

\documentclass[git]{test}
\usepackage[T1]{fontenc}

\begin{document}

|\verb|\myvar = |\texttt{\meaning\myvar}|

|\verb|\testgit = |\texttt{\meaning\testgit}|

|\ExpandArgs{e}\IfBlankTF{\myvar}{}{a}|
|\ExpandArgs{e}\IfBlankTF{\testgit}{}{b}|

\end{document}

output

5
  • Very enlightening! But why do you place a space ~ at the end of HEAD and -1 (when setting \endlinechar)? Commented Jun 11 at 21:56
  • @Gargantuar Sorry for the space after HEAD (that's redundant). The one after -1 isn't redundant.and actually recommended: it will disappear, but it's an important safety measure. Commented Jun 11 at 22:10
  • Moreover, what if \ProcessKeyOptions[test] was set inside \ExplSyntax...? Commented Jun 11 at 22:16
  • @Gargantuar Try it… Commented Jun 11 at 22:21
  • For the record: it's not working! Commented Jun 11 at 22:27

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.