1

A question inspired by @gernot's elegant answer in the topic Environment with a dummy parameter.

However some further experiments lead to the following. Indeed, what the devil? Take a @gernot version of the look-environment there and look at this.

\documentclass{article}
\begin{document}

\newenvironment{look}[1][\empty]%
{\begin{trivlist}\item[]\textsc{ThinkOut}% 
\ifx#1\empty\else\ (#1)\fi.\itshape}{\end{trivlist}}

\begin{verbatim}
\begin{look}[some addendum]
Text ...
\end{look}
\end{verbatim}
\begin{look}[some addendum]
Text ...
\end{look}

\begin{verbatim}
\begin{look}[q]
Seems allright? Look below ...
\end{look}
\end{verbatim}
\begin{look}[q]
Seems allright? Look below ...
\end{look}

\begin{verbatim}
\begin{look}[qq]
Double-q disappears. Where parentheses?
\end{look}
\end{verbatim}
\begin{look}[qq]
Double-q disappears. Where parentheses?
\end{look}

\begin{verbatim}
\begin{look}[aaa]
One of three a's survives; no ()'s do.
\end{look}
\end{verbatim}
\begin{look}[aaa]
One of three a's survives; no ()'s do.
\end{look}

\begin{verbatim}
\begin{look}[ qq]
Allright again.
\end{look}
\end{verbatim}
\begin{look}[ qq]
Allright again.
\end{look}

\begin{verbatim}
\begin{look}[{}aa]
Allright too.
\end{look}
\end{verbatim}
\begin{look}[{}aa]
Allright too.
\end{look}

\begin{verbatim}
\begin{look}[==]
Any double symbol disappears.
\end{look}
\end{verbatim}
\begin{look}[==]
Any double symbol disappears.
\end{look}

\end{document}

Or may be this is the known LaTeX bug?

PS. Sorry, I was not able to download the pdf-result file here.

PPS. The system I have is (info from a log-file)

e-TeX, Version 3.141592-2.1 (MiKTeX 2.3) (preloaded format=latex 2000.11.28)

LaTeX2e <2001/06/01> Document Class: article 2001/04/21 v1.4e

Standard LaTeX document class

  • 1
    With \begin{look}[qq] the \ifx test is \ifx qq\empty and obviously evaluates to true. What did you expect? Use \ifx\empty#1 instead. – egreg Feb 17 '17 at 11:44
  • Sorry for the typo/mistake, it has to read \empty#1 instead of #1\empty. – gernot Feb 17 '17 at 13:05

1 Answers1

3

No bug at all: with \begin{look}[qq] the test is

\ifx qq\empty

which obviously evaluates true.

The test should be

\ifx\empty#1\relax

but I'd prefer

\newenvironment{look}[1][]
  {%
   \begin{trivlist}
   \item\relax\textsc{ThinkOut}% 
   \if\relax\detokenize{#1}\relax
   \else
     \ (#1)%
   \fi.
   \itshape
  }
  {\end{trivlist}}

What happens with your version and \begin{look}[ab]?

The code does

\ifx ab\empty\else\ (ab)\fi

Since the test evaluates false, the false branch is followed. If you do \begin{look}[abcdefg], the test is

\ifx abcdefg\empty\else\ (abcdefg)\fi

and the tokens from c to \empty disappear as part of the true branch. If you try \begin{look}[aabbcc], the test is

\ifx aabbcc\empty\else\ (aabbcc)\fi

and you get bbcc printed.

David Carlisle
  • 757,742
egreg
  • 1,121,712
  • Sorry, I cannot understand why \begin{look}[ab] ... \end{look} works but \begin{look}[aa] ... \end{look} does not? So, only the very first twice-repeated symbol disappears. –  Feb 17 '17 at 11:51
  • @maximav Follow the conditionals. I added some examples. – egreg Feb 17 '17 at 11:55
  • @maximav with [aa] the ifx compares a and a which is true, with [ab] the \ifx compares a with b which is false. – David Carlisle Feb 17 '17 at 11:56
  • [ab] works fine for me under the old version. –  Feb 17 '17 at 12:05
  • @maximav that did not do \ifx#1 did it? – David Carlisle Feb 17 '17 at 12:11
  • I fixed a missing } after \itshape in @egreg's solution above but the system replies Incomplete \if. –  Feb 17 '17 at 12:13
  • @maximav the } was missing after #1 (I fixed it above) – David Carlisle Feb 17 '17 at 12:18
  • Seems it became work nicely! –  Feb 17 '17 at 12:25
  • I attempted to modify the updated version \look to usage \begin{list} instead of \begin{trivlist}. So I've changed \ifx#1\empty into \if\relax\detokenize{#1}\relax. Not work now unfortunately. An excessive empty parentheses in ThinkOut () I get when \begin{look} ... is called. What solution? –  Feb 17 '17 at 12:53
  • @maximav The syntax of list is quite different from trivlist – egreg Feb 17 '17 at 13:20