Re: Operatori unari alla C++ sui contatori TeX

#42951
Up
0
Down
::

Volendo scrivere un comando che incrementi un contatore TeX con la sintassi C++ (o Java), basta scrivere il seguente codice:`\documentclass{minimal}
\newcount\mycnt

\def\op#1++{%
\expandafter\advance\csname #1\endcsname by 1}

\begin{document}
\op mycnt++
\op mycnt++
\op mycnt++

\the\mycnt
\end{document}`

Come vedete il comando \op è un comando delimitato con il doppio ++ ed il suo nome sta per operator.
Il comando \op tuttavia accetta solo l’incremento, ma è possibile modificarlo:
Occorre che l’operatore di incremento o decremento sia reso variabile (++ oppure –).
Per non vincolare il numero dei caratteri del nome del contatore, un alternativa è quella di utilizzare un ulteriore delimitatore fisso: per esempio l’uguale:

\op mycnt=++
\op mycnt=–

Ecco il codice:
`\documentclass{minimal}

\newcount\mycnt

\def\pluschar{+}
\def\minuschar{-}
\def\op#1=#2#3{%
\def\tmp{#2}\def\tmpii{#3}
\ifx\tmp\tmpii
\ifx\tmp\pluschar
\expandafter\advance\csname #1\endcsname by 1
\else
\ifx\tmp\minuschar
\expandafter\advance\csname #1\endcsname by -1
\else
ERRORE: operatore non riconosciuto.
\fi
\fi
\else
ERRORE: simboli non uguali.
\fi}

\begin{document}
\op mycnt=++
\op mycnt=++
\op mycnt=++
\the\mycnt

\op mycnt=–
\op mycnt=–
\the\mycnt
\end{document}`

L’ideale sarebbe una sintassi di questo tipo:
`\newcount\xmycnt
\mycnt++
\mycnt–`

ovvero operare con comandi che sembrano nomi di contatori utilizzandone uno vero con un prefisso dietro le quinte:`\documentclass{minimal}

\def\pluschar{+}
\def\minuschar{-}
\def\newMagicCounter#1{%
\expandafter\newcount\csname x#1\endcsname
\expandafter\def\csname #1\endcsname##1##2{%
\def\tmp{##1}\def\tmpi{##2}
\ifx\tmp\tmpi
\ifx\tmp\pluschar
\expandafter\advance\csname x#1\endcsname by 1
\else
\ifx\tmp\minuschar
\expandafter\advance\csname x#1\endcsname by -1
\else
errore: operatore non riconosciuto
\fi
\fi
\else
errore: operatore disaccoppiato
\fi
}
}

\begin{document}
\newMagicCounter{mycnt}

\mycnt++
\mycnt++
\mycnt++
\mycnt++
\the\xmycnt

\mycnt–
\mycnt–
\the\xmycnt
\end{document}`

Vorrei chiedervi adesso due cose:
1 – trovate che la cosa sia simpatica?
2 – (più TeXnicamente) questo codice potrebbe nascondere degli effetti collaterali?
3 – si potrebbero implementare anche gli altri operatori tipo \mycnt=+6 ma è molto più difficile!!

Ciao. Alla prossima.

Prova questo:
`\documentclass{minimal}

\makeatletter
\newcount\@mycount

% Check if the following token is +
\def\mycount{\@ifnextchar+{\check@forplus}{\check@forother}}
% If it is check whether the next one is a + (we need to gobble the former +)
\def\check@forplus#1{\@ifnextchar+{\@advanceone}{\@advancemany}}
% Check for a – instead of a +
\def\check@forother{\@ifnextchar-{\check@forminus}{\@adverror}}
% If we had -, check for the next token
\def\check@forminus#1{\@ifnextchar-{\@advanceminusone}{\@advanceminusmany}}
% In case we have ++, gobble the second + and advance by 1
\def\@advanceone#1{\advance\@mycount\@ne}
% In case we have –, gobble the second – and advance by -1
\def\@advanceminusone#1{\advance\@mycount\m@ne}
% In case we have only one + or -, they should be followed by a number
\def\@advancemany{\advance\@mycount}
\def\@advanceminusmany{\advance\@mycount-}
% Throw an error if we have neither + nor – after \mycount
\def\@adverror{\errmessage{Wrong syntax}}

\def\Show{\showthe\@mycount}
\makeatother

\begin{document}
\mycount++
\Show

\mycount–
\Show

\mycount+6
\Show

\mycount-6
\Show

\end{document}`
Ciao
Enrico

Go to top