.png)
Estoy intentando dibujar piedras en una cuadrícula y mi macro API tendrá dos formas de hacerlo: o le das las coordenadas X e Y, o le das una cadena de coordenadas. Entonces, por ejemplo, ab
debería ser X = 1
y Y = 2
.
Siguienteesta respuesta de @PhelypeOleinik, ahora estoy intentando esto:
\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}
Pero me da este mensaje de error:
Improper alphabetic constant.
<to be read again>
¿Alguien sabe por qué? ¿Quizás el resultado es \StrRight
no producir algo que se considera un solo carácter? ¿Cómo puedo solucionar esto?
Respuesta1
Esto simplemente funciona mediante expansión, por lo que se puede usar en cualquier lugar donde se espere un número.
Muestro dos versiones, una que usa el clásico latex2e \@firstoftwo
y \expandafter
, y la segunda, una versión L3 más simple.
\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}