15

Is it possible to test within the document what commandline arguments were passed to tex? The particular case that prompts this is the question Tabs in output file written by xelatex and pdflatex are different where it would be useful to know if the -8bit argument has been passed to tex and issue a warning if not. That is, if pgfplots could contain the code:

\ifxetex
 \ifarg{8bit}
 \else
  \pkgwarning{You're using xe(la)tex without the -8bit argument.  Some external files might go wrong.  If things don't work as you expect, try using the -8bit argument.}
 \fi
\fi

then it might make it easier to spot when the situation of the linked question occurs.

(A year after asking the above question, it happened to me again and it took me some time to realise what the problem was and find the solution, even though it had been me who found it last time!)

2 Answers 2

12

I'm afraid the answer is "not directly".

One can define such a test:

\input catchfile.sty
\newif\ifeightbit
\begingroup
  \immediate\openout15=tab.tst
  \catcode`\^^I=12
  \immediate\write15{^^I}
  \immediate\closeout15
  \CatchFileDef\atab{tab.tst}{\catcode`\^=12 \catcode`I=12 }
  \edef\btab{\string^\string^\string I }
  \ifx\atab\btab
    \global\eightbitfalse
  \else
    \global\eightbittrue
  \fi
\endgroup
\show\ifeightbit

With xetex test (test.tex is the file name) we'll get \iffalse while with xetex -8bit test we'll get \iftrue.

It seems to be impossible to do this without writing a file, because \scantokens avoids the "translation" via the xchr tables. So while we can catch the three distinct characters when reading back the file, if written without the -8bit option, we can't do it with \scantokens.

3
  • I'm guessing from this that the answer to the general question is "No". Apr 17, 2012 at 20:20
  • You're guessing right. :)
    – egreg
    Apr 17, 2012 at 20:21
  • Fancy adding that to your answer? It was the actual question, after all! Apr 17, 2012 at 20:25
1

With --shell-escape:

(because of some permission issue this does not work on Overleaf, but otherwise should work on most POSIX systems)

\ExplSyntaxOn
\sys_get_shell:nnN {ps~-o~args=~-p~$(ps~-o~ppid=~-p~$$)} {\cctab_select:N \c_other_cctab} \l_data_tl
\ExplSyntaxOff

Quoted from my other answer in https://tex.stackexchange.com/a/621510/250119.

Then you need to parse the arguments somehow -- but be warned that parsing the arguments correctly is really difficult. A simple way is

\str_if_in:VnTF \l_data_tl {-8} {
  \typeout{>> probably~8bit}
} {
  \typeout{>> not~8bit}
}

but it's not foolproof, I run this simple bash file:

g(){
    echo "========" "$@"
    "$@" </dev/null | grep '>>\|!'
}
f(){
    g pdflatex --shell-escape "$@"
    g xelatex --shell-escape "$@"
}

mkdir -p -- --8bit a "a a"  # create auxiliary directories named literally "--8bit" and "a" and "a a"
ln -rsf a.tex "a --8bit b.tex"  # link "a.tex" -> a file literally named "a --8bit b.tex"
ln -rsf a.tex "b.tex"

# ======== these 3 cases return false:
f  a.tex
f  --output-di a "a --8bit b.tex"
f  --output-di --8bit a.tex
# ======== these 7 cases return true:
f  --output-di "a a" --8bit b.tex
f  -8bit a.tex
f  -8b a.tex
f  --8bit a.tex
f  --8bi a.tex
f  --8b a.tex
f  --8 a.tex
f  -8 a.tex

with a.tex contains:

% ======== copy from egreg's test ========
\input catchfile.sty
\newif\ifeightbit
\begingroup
  \immediate\openout15=tab.tst
  \catcode`\^^?=12
  \immediate\write15{^^?}
  \immediate\closeout15
  \CatchFileDef\atab{tab.tst}{\catcode`\^=12 \catcode`?=12 }
  \edef\btab{\string^\string^\string ? }
  \ifx\atab\btab
    \global\eightbitfalse
  \else
    \global\eightbittrue
  \fi
\endgroup




\ExplSyntaxOn
\cs_generate_variant:Nn \str_if_in:nnTF {V}
\sys_get_shell:nnN {ps~-o~args=~-p~$(ps~-o~ppid=~-p~$$)} {\cctab_select:N \c_other_cctab} \l_data_tl
\typeout{>> \l_data_tl}

\str_if_in:VnTF \l_data_tl {-8} {
  \typeout{>> probably~8bit}
} {
  \typeout{>> not~8bit}
}

\typeout{>> ifeightbit=\meaning\ifeightbit}
\ExplSyntaxOff

\stop

As you can see, the simple test outputs "probably 8bit" for the last 2 cases of the first block even though it isn't 8bit.

In particular, the 2nd and 4nd case makes the output identical with the provided command-line argument (you can fix this by not using ps -o args but I'm not sure how...)


With LuaLaTeX there's the arg table, but it looks like you'll still have to manually parse it:

\directlua{for _, v in ipairs(arg) do print(v) end}
\stop

From the other question in In Luatex, how to know effective output directory?.

But... in this specific case LuaTeX always ignore --8bit argument anyway:

luatex documentation

3
  • That is all above my head. But: Might it be possible to run a macro that produces different results, in the two cases, but does not print anything or throw an error? Then the logic could be forked, based on the result. No shell escape. I do something similar with lualatex and fontspec. After main font is set, I use \sbox0 with some text that has high and low points (such as Aacute and jyg). Then I measure \ht0 and \dp0 and fork, based on the length. Differentiates between "big glyph" fonts and "small glyph" fonts. I can even re-define font size and baselineskip.
    – rallg
    Apr 19, 2023 at 3:31
  • @rallg If you have some good idea why don't you post an answer? (I don't.)
    – user202729
    Apr 19, 2023 at 4:05
  • Sadly, I do not have any idea that addresses the question (as asked). My above comment is very user-specific (me). I just happen to be an advocate of lualatex and fontspec.
    – rallg
    Apr 19, 2023 at 17:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .