GeneralWonky Behaivor from \@ifnextchar

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
rspringuel
Posts: 19
Joined: Wed Apr 28, 2010 7:16 pm

Wonky Behaivor from \@ifnextchar

Post by rspringuel »

When I compile the below MWE the first \@ifnextchar, always evaluates to false, which is clearly wrong in the last case. However, the second one (within \punctuationcheck) works just fine. Why would the same conditional evaluate to False and then True?

Code: Select all

\documentclass{article}

\makeatletter
\newcommand{\punctuationcheck}{%
	\@ifnextchar){I see a parens\relax}{%
	\@ifnextchar]{I see a square bracket\relax}{%
	\@ifnextchar.{I see a period\relax}{%
	\@ifnextchar,{I see a comma\relax}{I didn' t see anything\ }}}}%
}
\newcommand{\ie}{\textit{i.e.}\@ifnextchar,{I saw a comma\relax}{I didn't see a comma,}\punctuationcheck}
\makeatother

\begin{document}
\ie) test

\ie] test

\ie. test

\ie, test
\end{document}
What this produces when I compile it:
i.e.I didn’t see a comma,I see a parens) test
i.e.I didn’t see a comma,I see a square bracket] test
i.e.I didn’t see a comma,I see a period. test
i.e.I didn’t see a comma,I see a comma, test

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

rspringuel
Posts: 19
Joined: Wed Apr 28, 2010 7:16 pm

Wonky Behaivor from \@ifnextchar

Post by rspringuel »

Got an answer over on stackexchange. Turns out that the \@ifnextchar, within \ie was checking \punctuationcheck rather than the actual text. In order for it to look back at the actual text, I had to move \punctuationcheck inside the code tokens so that there was nothing after the three arguments to \@ifnextchar within \ie.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Wonky Behaivor from \@ifnextchar

Post by Johannes_B »

If i had known, that you posted the question on Stackexchange, i would have checked if there are comments or anwsers already. Please avoid crossposting, or at least link the posts. Seems i have wasted some of my time. Please have a look at Gimme Pizza

Anyway, here is what i wrote up last night:



\@ifnextchar never got to see the comma, nor the period, nor the bracket and so on. What the command saw as the next char was \punctuationcheck.

What are you trying to achieve? We might find another solution.

Code: Select all

\documentclass{article}

\makeatletter
\newcommand{\punctuationcheck}{%
	\@ifnextchar){I saw a parens\relax}{%
	\@ifnextchar]{I saw a square bracket\relax}{%
		\@ifnextchar.{I saw a period\relax}{%
			\@ifnextchar,{I saw a comma\relax}{I didn' t see anything\
			}%
		}%
	}%
}%
}
\def\true{TRUE}
\def\false{FALSE}
\newcommand\ie{\@ifnextchar,{\true}{\false},}
\newcommand\ih{\@ifnextchar,{\true}{\false} }
\newcommand{\ig}{\textit{i.e.\space}\@ifnextchar,{I saw a comma\space\punctuationcheck}{I didn't see a comma,\space\punctuationcheck}}

\makeatother

\begin{document}
\ie) test \par
\ie] test \par
\ie. test \par
\ie, test \par\medskip
\ih) test \par
\ih] test \par
\ih. test \par
\ih, test \par\medskip
\ig) test \par
\ig] test \par
\ig. test \par
\ig, test \par

\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply