MakeIndex, Nomenclature, Glossaries and AcronymsIndex help with species names: italics and subentries

Information and discussion about MakeIndex - the tool to generate subject indices for LaTeX documents.
Post Reply
cara
Posts: 8
Joined: Wed Nov 25, 2009 10:40 am

Index help with species names: italics and subentries

Post by cara »

I'm having a problem with scientific species names and my index.
The species names should be in italics and with subentries if there is more than one species (e.g. Rana dunni, Rana onca) from the same genus (e.g. Rana).
Meaning I would like to have the entry Rana with the subentries dunni and onca
I don't want all the species names at the beginning of the index (the command

Code: Select all

\textit{Rana dunni}\index{\textit{Rana!dunni}}
does that), but I'd rather have the species names in the index with all the other index entries (e.g. dolor, vivamus).
So I changed the command to

Code: Select all

\textit{Rana dunni}\index{Rana@\textit{Rana!dunni}}
Additionally this changed command allows me to format the index entry number with an additional |textbf or |textit.

The problem is that it works fine with one entry (Ranna dunni), but when I add the second entry (Ranna onca) it still works but I get the error:
! Extra }, or forgotten \endgroup
And I have quite a lot of species names to deal with so I'd rather solve this than have tons of error messages.

Here is my MWE:

Code: Select all

\documentclass[a4paper]{article}

\usepackage[english]{babel}

\usepackage{makeidx}
 \makeindex 


\begin{document}
 Lorem ipsum dolor\index{dolor} sit amet, consectetuer adipiscing elit.
 Vivamus\index{vivamus} elementum semper nisi.
 
\textit{Rana dunni}\index{Rana@\textit{Rana!dunni}} 

\textit{Rana onca}\index{Rana@\textit{Rana!onca}}

\printindex

\end{document}
I'm using MikTeX, TeXnicCenter 1.0 Stable Release Candidate 1 and I'm compiling to dvi.

Help would be really appreciated.
Thx & Cheers
edited to put the two commands in code environment
Last edited by cara on Fri Jul 30, 2010 7:40 pm, edited 2 times in total.

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

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Index help with species names: italics and subentries

Post by localghost »

At first thanks for this very good minimal example and the problem description. You cannot put the main entry and sub-entry together with the »!« separator as argument for the text formatting command. You have to format them separately. See below for the relevant parts of your example.

Code: Select all

\textit{Rana dunni}\index{Rana@\textit{Rana}!\textit{dunni}}
\textit{Rana onca}\index{Rana@\textit{Rana}!\textit{onca}}
But it seems a little bit uncomfortable to always type this when marking an index entry. So it's better to define a new command which needs a mandatory argument for the main entry and accepts an optional one for the possible sub-entry.

Code: Select all

\newcommand*{\itindex}[2][]{\textit{#2 #1}\index{#2@\textit{#2}\ifx#1\empty\else!\textit{#1}\fi}}
The line above goes into the preamble. Now you only have to use this new command in your document as follows.

Code: Select all

\itindex{Rana}

\itindex[dunni]{Rana}

\itindex[onca]{Rana}
The new command will show the arguments in the text and simultaneously put them into the index (in opposite to the original \index command). You can define similar commands for the other font styles.


Best regards and welcome to the board
Thorsten
cara
Posts: 8
Joined: Wed Nov 25, 2009 10:40 am

Index help with species names: italics and subentries

Post by cara »

Thanks very much for the prompt and perfect solution to my problem!

It now works beautifully! You made my day.


I just wanted to ask if you could explain the command a bit, so that I can hopefully one day make my own new commands,

Code: Select all

\newcommand*{\itindex}[2][]{\textit{#2 #1}\index{#2@\textit{#2}\ifx#1\empty\else!\textit{#1}\fi}}
or if you could point me in the right direction for more documentation on this sorta thing?


Thx a bunch & Cheers
cara
Posts: 8
Joined: Wed Nov 25, 2009 10:40 am

Index help with species names: italics and subentries

Post by cara »

I have a problem with the new command

Code: Select all

\newcommand*{\itindex}[2][]{\textit{#2 #1}\index{#2@\textit{#2}\ifx#1\empty\else!\textit{#1}\

\itindex{Rana}

\itindex[dunni]{Rana}

\itindex[onca]{Rana}
because after the first mention I abbreviate my species names.
So Ranna dunni would be R. dunni after the first mention, but I need the full name in the index.

The command you made replaces the whole species name and does not work like the normal \index command.

The normal \index command in the text would look like this:

Code: Select all

\textit{R. dunni}\index{Rana dunni}
but the new \itindex command looks like this

Code: Select all

\itindex[dunni]{Ranna}
Is there any way to make the \itindex command behave like the \index command so that I can just add the \itindex command behind my species names no matter if they are abbreviated or not?

MWE

Code: Select all

\documentclass[a4paper]{article}
\usepackage[english]{babel}

\usepackage{makeidx}
\makeindex
\newcommand*{\itindex}[2][]{\textit{#2 #1}\index{#2@\textit{#2}\ifx#1\empty\else!\textit{#1}\fi}}

\begin{document}
Lorem ipsum dolor\index{dolor} sit amet, consectetuer adipiscing elit.
Vivamus\index{vivamus} elementum semper nisi.
\itindex{Rana}
\itindex[onca]{Rana}

\itindex[dunni]{Rana}
\textit{R.~dunni}\itindex[dunni]{Rana}

\printindex

\end{document}
Thx
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Index help with species names: italics and subentries

Post by localghost »

cara wrote:I have a problem with the new command [...] because after the first mention I abbreviate my species names. []
That complicates things a little bit (and should have been mentioned right at the beginning).
cara wrote:[...] The command you made replaces the whole species name and does not work like the normal \index command. [...]
I'm well aware. And I explained that already.
cara wrote:[...] Is there any way to make the \itindex command behave like the \index command so that I can just add the \itindex command behind my species names no matter if they are abbreviated or not?[...]
Another idea is to extend the command so that it takes one more optional argument. I tinkered some time with different mechanisms but none of them worked satisfactory. So I used the xifthen and xargs package.

Very astonishing and also notable is the fact that you always have to give the first optional argument to the command (even if it is empty). Otherwise it doesn't work. Utilization of the new command \itidx is shown below.

Code: Select all

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{makeidx}
\usepackage{xifthen,xargs}

\makeindex

\newcommandx*{\itidx}[3][1=,2=]{%
  \textit{\ifthenelse{\isempty{#1}}{#3 #2}{#1. #2}}\index{#3@\textit{#3}\ifx#2\empty\else!\textit{#2}\fi}
}

%\newcommand*{\itindex}[2][]{\textit{#2 #1}\index{#2@\textit{#2}\ifx#1\empty\else!\textit{#1}\fi}}


\begin{document}
  Lorem ipsum dolor\index{dolor} sit amet, consectetuer adipiscing elit. Vivamus\index{vivamus} elementum semper nisi.

  The \itidx{Rana}

  The \itidx[][dunni]{Rana}

  The \itidx[][onca]{Rana}

  The \itidx[R][dunni]{Rana}

  \printindex
\end{document}
It might also be worth to think about a solution with the xkeyval package.

Note: If somebody has a similar solution perhaps without or with less additional packages, I would be very interested.
cara
Posts: 8
Joined: Wed Nov 25, 2009 10:40 am

Index help with species names: italics and subentries

Post by cara »

Thx a lot. This new command works fine in the index, but it is a bit problematic in the text, because it inserts empty space after the species names. This is fine except when the species name is followed by a comma or period.

I already tried to insert the command

Code: Select all

$\!$
(sometimes multiple times )after the \itidx command finished in the text

Code: Select all

\itidx[][dunni]{Rana}$\!$
to get rid of the space between the species names and commas or periods.

And it works sometimes, but other times I have the species name at the end of the line and the comma or period can be found at the beginning of the next line.
Furthermore the command

Code: Select all

$\!$
is incredibly clumsy and even if it works, it doesn't look like it's supposed to be (the comma/period is too close etc.).

So i would be really grateful if you'd know a way around this.

Code: Select all

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{makeidx}
\usepackage{xifthen,xargs}

\makeindex

\newcommandx*{\itidx}[3][1=,2=]{%
  \textit{\ifthenelse{\isempty{#1}}{#3 #2}{#1. #2}}\index{#3@\textit{#3}\ifx#2\empty\else!\textit{#2}\fi}
}

%\newcommand*{\itindex}[2][]{\textit{#2 #1}\index{#2@\textit{#2}\ifx#1\empty\else!\textit{#1}\fi}}


\begin{document}
  Lorem ipsum dolor\index{dolor} sit amet, consectetuer adipiscing elit. Vivamus\index{vivamus} elementum semper nisi.

 Lorem ipsum dolor sit amet, consectetuer adipiscing elit elit. The \itidx{Rana}, Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

  The \itidx[][dunni]{Rana}, Lorem ipsum dolor sit amet, consectetuer adipiscing elit, Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

  The \itidx[][onca]{Rana}.
  Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

  The \itidx[R][dunni]{Rana}.

  \printindex
\end{document}
Thx a lot for your patience!!!
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Re: Index help with species names: italics and subentries

Post by localghost »

I tried several approaches, but without success. At the moment I can't continue working on that because other tasks require my attention. Perhaps somebody else can help. For the moment it seems that have to use the traditional method.
User avatar
nlct
Posts: 276
Joined: Thu Nov 06, 2008 11:15 am

Index help with species names: italics and subentries

Post by nlct »

Try the following definition of \itidx:

Code: Select all

\newcommandx*{\itidx}[3][1=,2=]{%
  \index{#3@\textit{#3}\ifx#2\empty\else!\textit{#2}\fi}%
  \textit{\ifthenelse{\isempty{#1}}{#3%
    \ifthenelse{\isempty{#2}}{}{ #2}}{#1.%
    \ifthenelse{\isempty{#2}}{}{\ #2}}}%
}
Regards
Nicola Talbot
Post Reply