
我正在嘗試在網格上繪製石頭,我的宏 API 將有兩種方法來實現:要么給出 X 和 Y 坐標,要么給出坐標字串。因此,例如,ab
應該是X = 1
和Y = 2
。
下列的@PhelypeOleinik 的回答,我現在正在嘗試這個:
\documentclass{article}
\usepackage{xstring}
\newcommand\makeFromAlph[1]{
\number\numexpr`#1-`a\relax % I should probably lowercase `#1` first...
}
% I've also tried this version:
% \ExplSyntaxOn
% \NewExpandableDocumentCommand \makeFromAlph { m }
% { \int_eval:n { \int_from_alph:n {#1} - 1 } }
% \ExplSyntaxOff
\begin{document}
\StrRight{ab}{1} % This works
\makeFromAlph{\StrRight{ab}{1}} % But this doesn't
\end{document}
但它給了我這個錯誤訊息:
Improper alphabetic constant.
<to be read again>
有誰知道為什麼?輸出\StrRight
是否可能不產生被視為單一字元的東西?我該如何解決?
答案1
這只是透過擴展起作用,因此可以在任何需要數字的地方使用。
我展示了兩個版本,一個使用經典的 Latex2e\@firstoftwo
和\expandafter
,第二個是更簡單的 L3 版本。
\documentclass{article}
\makeatletter
\newcommand\foo[1]{%
The first number is \the\numexpr \expandafter`\@firstoftwo#1-96\relax,
The second number is \the\numexpr \expandafter`\@secondoftwo#1-96\relax.
}
\makeatother
\ExplSyntaxOn
\cs_generate_variant:Nn \int_from_alph:n {e}
\newcommand\foob[1]{
\int_from_alph:e{\use_i:nn#1}, ~\int_from_alph:e{\use_ii:nn#1}.
}
\ExplSyntaxOff
\begin{document}
\foo{ab}
\foob{ab}
\end{document}