0

I'm trying to draw stones on a grid, and my macro API will have two ways of doing it: either you give the X and Y coordinates, or you give it a string of the coordinates. So, for example, ab should be X = 1 and Y = 2.

Following this answer by @PhelypeOleinik, I'm now trying this:

\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}

But it's giving me this error message:

Improper alphabetic constant.
<to be read again> 

Does anyone know why? Is the output from \StrRight not yielding something that's considered a single character maybe? How do I fix this?

3
  • 2
    there are really much more powerful commands pre-defined in the latex format than available in xstring. If you have old package code using it, it's Ok, but I wouldn't use it in new code Commented Feb 6 at 19:00
  • you can use \@firstoftwo ab and \@secondoftwo ab to get a and b respectively by expansion without using xstring (or L3) Commented Feb 6 at 19:03
  • Can you give an example of input and what will be drawn? L3 allows doing a lot of things easily.
    – projetmbc
    Commented Feb 6 at 19:03

1 Answer 1

4

enter image description here

This just works by expansion so can be used anywhere a number is expected.

I show two versions, one using classic latex2e \@firstoftwo and \expandafter, and secondly a simpler L3 version.

\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}

5
  • I've added an edit of how I adapted your solution to what I'm doing right now. What do you think? Quite frankly, I'm not sure if I've fully understood everything there, namely why \expandafter, \makeatletter, and \makeatother are necessary.
    – psygo
    Commented Feb 6 at 20:59
  • @PhilippeFanaro if you want that addition to be an answer to something you should have put something in the question asking for that, preferably some complete not working document, if you had, I'd have added it, as it is it's just code that only you could add, not an answer to the question. (You could still edit the question to fit the extended answer) Commented Feb 6 at 21:02
  • I was using xstring because that's what pops up when you Google that type of question, I didn't know it was kinda very outdated.
    – psygo
    Commented Feb 6 at 21:03
  • I know the example I added is an "unnecessary" extra. But I think it would help people who read the answer in the future anyways.
    – psygo
    Commented Feb 6 at 21:19
  • @PhilippeFanaro as I say if you want that to be an answer you need to edit the question, as it is it just looks like confusing unexplained code that does not relate to the question at all, you may know that use case was behind the question but no one else will. Commented Feb 6 at 21:21

You must log in to answer this question.

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