GeneralDoes Latex introduce spaces in #1?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
bers
Posts: 6
Joined: Mon Feb 08, 2010 12:33 am

Does Latex introduce spaces in #1?

Post by bers »

Hi,
in this code:

Code: Select all

\documentclass{article}
\usepackage{nomencl}
\makeindex
\makenomenclature
\begin{document}

\nomenclature[\textrm{a}]{b}{c}

\newcommand{\nomenclatureTwo}[3]{\nomenclature[#1]{#2}{#3}}
\nomenclatureTwo{\textrm{d}}{e}{f}

Test
\printnomenclature
\end{document}
I noticed the following: The resulting test.nlo file looks like this:

Code: Select all

\nomenclatureentry{\textrm{a}b@[{b}]\begingroup c\nomeqref {0}|nompageref}{1}
\nomenclatureentry{\textrm  {d}e@[{e}]\begingroup f\nomeqref {0}|nompageref}{1}
This is weird. Where do the spaces come from? It disturbs the sorting order - can I prevent the spaces from appearing?

Thanks,
bers

Recommended reading 2024:

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

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

josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Does Latex introduce spaces in #1?

Post by josephwright »

No, the spaces are added by TeX as you are writing to a file. When you write something like

Code: Select all

\foo{text}
to file, the category codes are lost. So TeX always includes a space after each control sequence when it does this, resulting in

Code: Select all

\foo {text}
with the idea being that this space will disappear when TeX reads back the file.
Joseph Wright
bers
Posts: 6
Joined: Mon Feb 08, 2010 12:33 am

Re: Does Latex introduce spaces in #1?

Post by bers »

Hi,
thanks your reply. However, I still don't understand why the spaces only appear in one line, and not in the other. And what is more important for me, what I can do to have both lines appear similarly.
Last edited by bers on Sat Sep 18, 2010 12:58 pm, edited 1 time in total.
bers
Posts: 6
Joined: Mon Feb 08, 2010 12:33 am

Does Latex introduce spaces in #1?

Post by bers »

Hi again,
I extended my example like this:

Code: Select all

\documentclass{article}
\usepackage{nomencl}
\makeindex
\makenomenclature
\begin{document}

\newcommand{\nomenclatureOne}[2]{\nomenclature[]{#1}{#2}}
\newcommand{\nomenclatureTwo}[3]{\nomenclature[#1]{#2}{#3}}

\nomenclature[\textrm{1}]{\textrm{1}}{1} % no space
\nomenclatureOne{\textrm{2}}{2} % 1 space
\nomenclatureTwo{\textrm{3}}{\textrm{3}}{3} % 2 spaces

Test
\printnomenclature
\end{document}
Now, the nlo file looks like this -- note that now there is no space, 1 space, and also 2 spaces:

Code: Select all

\nomenclatureentry{\textrm{1}\textrm{1}@[{\textrm{1}}]\begingroup 1\nomeqref {0}|nompageref}{1}
\nomenclatureentry{\textrm {2}@[{\textrm {2}}]\begingroup 2\nomeqref {0}|nompageref}{1}
\nomenclatureentry{\textrm  {3}\textrm {3}@[{\textrm {3}}]\begingroup 3\nomeqref {0}|nompageref}{1}
Also, the sort order in the nomenclature is 3, 2, 1, while it should be 1, 2, 3. Is there ANYTHING I can do to make the sort order right?

Thanks,
bers
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Does Latex introduce spaces in #1?

Post by josephwright »

Yup, don't try to wrap \nomenclature inside another macro. Reading the code that makes this work, I see that \nomenclature needs to alter the category code of material it reads in a verbatim-like way. It's never safe to wrap up commands that work in a verbatim way inside other commands.

Notice that in the manual there is no direct warning of this but it does say
The nomencl package tries hard to write the arguments of the \nomenclature macro verbatim to the glossary file.
Joseph Wright
bers
Posts: 6
Joined: Mon Feb 08, 2010 12:33 am

Re: Does Latex introduce spaces in #1?

Post by bers »

Ok, thanks again. Not very helpful, but informative :)

For everyone else interested: The problem I pointed out exists in two flavors.

1. nomenclature vs \newcommand: josefwrights answers apply.
2. nomenclature[#1]{#2}{#3} vs nomenclature{#2}{#3}:
nomencl treats #1 without, and #2 with \nom@verb, hence the *second* space in #1. I filed this under http://sarovar.org/tracker/index.php?fu ... 4&atid=261, but I have little confidence that this will be fixed.

In my work, I fixed both by ONLY using nomenclature through a macro (let's call is mynomencl), and by always putting #2 into the square brackets of \nomenclature when using mynomencl{#2}{#3}. This way, nomencl treats all the beginning of all sorting keys identically.
Post Reply