문자열 좌표를 숫자 좌표로 변환(예: ab -> 1 및 2)

문자열 좌표를 숫자 좌표로 변환(예: ab -> 1 및 2)

저는 그리드에 돌을 그리려고 하는데 매크로 API에는 이를 수행하는 두 가지 방법이 있습니다. X 및 Y 좌표를 제공하거나 좌표 문자열을 제공하는 것입니다. 따라서 예를 들어 및 ab이어야 합니다 .X = 1Y = 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와 .NET을 사용하고 \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}

관련 정보