2

I'm currently working on making a kind of textbook for Japanese math learners. Then, I made a counter macro as below.

It seems to work nice in normal environment, as far as I know. But once I put it into align environment, it will no longer work correctly. I think it is double counted; for instance, when I use \utoi three times, it will start from the fourth letter.

I would appreciate it very much if anyone could fix it. Thanks in advance.

\documentclass[fleqn, uplatex]{jsarticle}
\usepackage{amsmath}
\global\newcount\kAnAc
\def\Cleariroha{\kAnAc=0}
\Cleariroha
\makeatletter
\def\iroha{\@ifnextchar [{\K@N@@}{\K@N@}}
\def\K@N@{%
 \global\advance\kAnAc\@ne%
 \ifcase\kAnAc ×\or イ\or ロ\or ハ\or ニ\or ホ\or ヘ\or ト\or チ\or リ\or ヌ\or ル\or ヲ\or ワ\or カ\or ヨ\or タ\or レ\or ソ\or ツ\or ネ\or ナ\or ラ\or ム\or ×\fi%
}
\def\K@N@@[#1]{%
 \batchmode%
 \ifcase\ref{#1} ×\or イ\or ロ\or ハ\or ニ\or ホ\or ヘ\or ト\or チ\or リ\or ヌ\or ル\or ヲ\or ワ\or カ\or ヨ\or タ\or レ\or ソ\or ツ\or ネ\or ナ\or ラ\or ム\or ×\fi%
\errorstopmode}%
\def\Klabel#1{\@bsphack
  \protected@write\@auxout{}%
         {\string\newlabel{#1}{{\the\kAnAc}{\thepage}}}%
  \@esphack}
\makeatother
\newcommand{\utoi}[1]{\underset{\iroha}{\underline{#1}}}
\begin{document}
$\utoi{3}$, $\utoi{5}$
\Cleariroha
\begin{align*}
    y&=\utoi{a}x+\utoi{b}\\
    &=\utoi{5}x+\utoi{4}
\end{align*}
\end{document}

Just in case: In order to run Japanese language in TeX file, you need to make the following file as "latexmkrc"

$latex = 'uplatex';
$bibtex = 'upbibtex';
$dvipdf = 'dvipdfmx %O -o %D %S';
$makeindex = 'mendex -U %O -o %D %S';
$pdf_mode = 3; 
1
  • Welcome to TEX.SE! As long as Japanese isn't really strictly necessary couldn't you edit the MWE by just using something like \the\kAnAc? Anyway the problem is due to the fact that align typesets its argument twice, hence the double counting. A possible workaround using amsmath's internal macro \@ifmeasuring@ is shown in this answer.
    – campa
    Sep 22, 2020 at 7:27

2 Answers 2

2

Unrelated but don't do \global\newcount\kAnAc it's just by chance that \global does not give an error it does not make any assignment global that is not already global.

AMS alignments are evaluated twice, to measure the various alignment widths before the final setting. The package saves and restores all LaTeX counters to prevent them being double incremented but it does not do this with lower level conters allocated with \newcount.

So you can replace

\global\newcount\kAnAc

by

\newcounter{kAnAc}

and

\global\advance\kAnAc\@ne%
 \ifcase\kAnAc

by

\stepcounter{kAnAc}%
 \ifcase\value{kAnAc}%

Alternatively keep the code as you have it, but only increment on the measuring pass:

\ifmeasuring@
  \global\advance\kAnAc\@ne
\fi
1
  • I made it finally. You really helped me a lot. I thank you very much.
    – Taka
    Sep 22, 2020 at 8:05
0

You need to advance the counter only once in multiline displays, because amsmath does two passes over the material.

This can be done by checking whether \ifmeasuring@ is true or false. You need to do the stepping only when false, so \utoi would work also inside equation, where the measuring is not done. Since Kana's have the same width, this should not be a big issue. To fix the possible issues, I made \iroha return also in the case 0, which only happens when you do \iroha[label] and the label has not yet been resolved (in this case \getrefnumber returns 0).

I also suggest a different way to cope with the display of Kana's without the horrible \batchmode trick.

\documentclass[fleqn, uplatex]{jsarticle}
\usepackage{amsmath}
\usepackage{refcount}

\makeatletter
\newcommand{\K@N@numbering}[1]{%
  \ifcase#1\relax
    イ\or イ\or ロ\or ハ\or ニ\or ホ\or ヘ\or ト\or チ\or リ\or
   ヌ\or ル\or ヲ\or ワ\or カ\or ヨ\or タ\or レ\or ソ\or ツ\or
   ネ\or ナ\or ラ\or ム\or ×\fi
}
\newcounter{kAnAc}
\newcommand\Cleariroha{\setcounter{kAnAc}{0}}
\Cleariroha

\makeatletter
\newcommand{\iroha}[1][]{%
  \if\relax\detokenize{#1}\relax
    % no optional argument
    \ifmeasuring@\else\stepcounter{kAnAc}\fi
    \K@N@numbering{\value{kAnAc}}%
  \else
    \K@N@numbering{\getrefnumber{#1}}%
  \fi
}
\def\Klabel#1{\@bsphack
  \protected@write\@auxout{}%
         {\string\newlabel{#1}{{\the\kAnAc}{\thepage}}}%
  \@esphack}
\makeatother

\newcommand{\utoi}[1]{\underset{\iroha}{\underline{#1}}}

\begin{document}

$\utoi{3}$, $\utoi{5}$
\Cleariroha
\begin{align*}
    y&=\utoi{a}x+\utoi{b}\\
    &=\utoi{5}x+\utoi{4}
\end{align*}

\end{document}

enter image description here

You must log in to answer this question.

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