TeX - LaTeX Asked by Patrik Bak on March 6, 2021
Assume I have a file named G1 (SK).tex
containing
defSK{SK}
defparse"#1 (#2)"{deflanguage{#2}}
expandafterparsejobname
ifxSKlanguage equalelse distinctfi
bye
When I run it, it prints distinct
, even if the value of jobname
is "G1 (SK)"
. When I use
expandafterparse"G1 (SK)"
it works as expected and prints equal
.
How can I fix this?
jobname
produces characters of category code 12, while the replacement text of SK
contains characters of category code 11.
There are several ways to cope with the problem. If you use pdftex
that supports e-TeX extensions
edefSK{detokenize{SK}}
defparse"#1 (#2)"{deffilelanguage{#2}}
expandafterparsejobname
ifxSKfilelanguage equalelse distinctfi
bye
You could also exploit pdfstrcmp
:
defparse"#1 (#2)"{deffilelanguage{#2}}
expandafterparsejobname
ifnumpdfstrcmp{SK}{filelanguage}=0 equalelse distinctfi
bye
because pdfstrcmp
does string comparison independent of category codes (and expands macros in its arguments).
In any case, you shouldn't do deflanguage
, because language
is a TeX primitive.
A more flexible solution with expl3
.
input expl3-generic
ExplSyntaxOn
str_new:N l_bak_file_language_str
str_set_eq:NN l_bak_file_language_str c_sys_jobname_str
regex_replace_once:nnN { .*? ((.*)) .* } { 1 } l_bak_file_language_str
% now the string variable contains the string in parentheses
% extracted from the jobname
cs_new:Npn checklanguage
{
str_case:VnF { l_bak_file_language_str }
{
{SK}{Language~is~SK}
{AB}{Language~is~AB}
{XYZ}{Language~is~XYZ}
}
{Undefined~language}
}
ExplSyntaxOff
checklanguage
bye
This prints “Language is SK”, but if I use a different jobname, say XYZ(X)
, I get “Undefined language”.
The command checklanguage
is fully expandable and so works in edef
. The token to execute for each string are up to you and your intended application. Note that you don't need to care about the quotes added if there is a space in the name, because the first lines just extract what's between the (first set of) parentheses.
Correct answer by egreg on March 6, 2021
jobname
assigns category code 12 to all characters except space when it's set. You can get your desired result doing something like this:
{
catcode`S=12
catcode`K=12
gdefsk{SK}
}
defparse"#1 (#2)"{deflanguage{#2}}
expandafterparsejobname
ifxsklanguage equalelse distinctfi
bye
Answered by Don Hosek on March 6, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP