Latex 中宏的强制扩展

Latex 中宏的强制扩展

在将宏作为参数 之前如何强制\myget{second}扩展,这对我来说似乎是个问题。按应有的方式工作。\qrcode{xxx}\qrcode{foobar}

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{qrcode}
\usepackage{xstring}

\def\Testmap{
  {first}{blue}
  {second}{red}
  {foo.bar}{orange}
  {4}{violet}
  {bla}{green}
  {6}{violet}
  {7}{violet}
  {8}{black}
}

\def\xget#1#2{\expandafter\xgetimpl#1{#2}{}\xget{#1}{#2}}%
\def\xgetimpl#1#2#3\xget#4#5{%
\IfEq{#5}{#1}{#2}{\xgetimpl#3\xget{#4}{#5}}%
}%

\protect\def\myget#1{\xget{\Testmap}{#1}}%

\begin{document}

\qrcode{\myget{second}}    %%%% !!!!!!!!!!!!
% \myget{second}           %%%% this works fine (prints 'red' as expected)

\end{document}

我尝试在这段代码的几个地方放置几个\protect,\expandafter和 whatever,但都不起作用,也没有提示我下一步该看哪里。使用\edef\tmp{\myget{second}}也没什么用。

以下是 pdflatex *.log 文件的相关部分:

! Undefined control sequence.
\in@ #1#2->\begingroup \def \in@@ 
                  ##1#1{}\toks@ \expandafter {\in@@ #2{}{}#1...
l.27 \qrcode{\myget{second}}

The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! Illegal parameter number in definition of \qr@texttoencode.
<to be read again> 
          1
l.27 \qrcode{\myget{second}}

You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I'm going to assume that you meant ##.

! Undefined control sequence.
\in@ ...f \in@@ ##1#1{}\toks@ \expandafter {\in@@ 
                          #2{}{}#1}\edef \in@@ {\the...
l.27 \qrcode{\myget{second}}

The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

! Undefined control sequence.
\in@ ... \expandafter {\in@@ #2{}{}#1}\edef \in@@ 
                          {\the \toks@ }\expandafter...
l.27 \qrcode{\myget{second}}

The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

)
! Incomplete \iffalse; all text was ignored after line 27.
<inserted text> 
        \fi 
<*> test.tex

The file ended while I was skipping conditional text.
This kind of error happens when you say `\if...' and forget
the matching `\fi'. I've inserted a `\fi'; this might work.

! Emergency stop.
<*> test.tex

*** (job aborted, no legal \end found)

答案1

l3prop为此,我建议使用属性列表。对其元素的访问是完全可扩展的,因此可以在参数中使用。

\documentclass{article}
\usepackage{qrcode}
\usepackage{xparse}

\ExplSyntaxOn

\prop_new:N \l_stefan_testmap_prop

\prop_put:Nnn \l_stefan_testmap_prop {first} {blue}
\prop_put:Nnn \l_stefan_testmap_prop {second} {red}
\prop_put:Nnn \l_stefan_testmap_prop {foo.bar} {orange}
\prop_put:Nnn \l_stefan_testmap_prop {4} {violet}
\prop_put:Nnn \l_stefan_testmap_prop {bla} {green}
\prop_put:Nnn \l_stefan_testmap_prop {6} {violet}
\prop_put:Nnn \l_stefan_testmap_prop {7} {violet}
\prop_put:Nnn \l_stefan_testmap_prop {8} {black}

\DeclareExpandableDocumentCommand \myget { m }
 {
  \prop_item:Nn \l_stefan_testmap_prop { #1 }
 }

\ExplSyntaxOff

\begin{document}

\qrcode{\myget{second}}

\end{document}

在此处输入图片描述

答案2

您需要一些可扩展的宏。顺便说一下,\protectMWE 中的 不应该在那里。它没有任何效果,如果用正确的 替换\protected它,它会产生内部不可用的效果\qrcode \myget

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{qrcode}
\usepackage{xinttools}

\xintForpair #1#2 in {
  (first, blue),
  (second, red),
  (foo.bar, orange),
  (4, violet),
  (bla, green),
  (6, violet),
  (7, violet),
  (8, black)
}\do {\expandafter\def\csname Testmap_#1\endcsname {#2}}

\def\myget #1{\csname Testmap_\xintZapSpaces{#1}\endcsname}

\begin{document}

\textcolor{\myget{first}}{\qrcode{\myget{first}}}    %%%% ok

\textcolor{\myget{second}}{\qrcode{\myget{second}}}    %%%% ok

\end{document}

引用

相关内容