GeneralConditional IF with characters (INDEXING)

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
NinV
Posts: 69
Joined: Sun Oct 05, 2008 12:57 pm

Conditional IF with characters (INDEXING)

Post by NinV »

Hi,

sorry for bothering you, but I didn't succeed to find a solution in my guides and in this forum to my problem, which I didn't think so difficult!

I have to check if the first character of a string is "-" or not.

I wrote the following code

Code: Select all

\def\t{-}
\def\d{\estraiiniziale{#1}} % I put the first character in \d

\ifx\t\d
  ....
\else
  ....
\fi
but it doesn't work!

Can somebody help me?

Many thanks,
Nino

PS I need to make an index where all words beginning with a 'symbol' are sorted according to the following letter. Perhaps there is a simpler way to do that, but I failed to find it :(
Window + MiKTeX 2.7
Ubuntu + TexLive + Kile
Xandros (eeePC) + TeTex + TexMaker

Recommended reading 2024:

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

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

nlct
Posts: 276
Joined: Thu Nov 06, 2008 11:15 am

Conditional IF with characters (INDEXING)

Post by nlct »

You could define a command like:

Code: Select all

\newcommand*{\word}[1]{-#1\index{#1@-#1}}
then you can do \word{sample} which would display -sample in the text and index -sample but sort it without the - symbol. Alternatively, there are packages like datatool that can check for a given prefix.

Regards
Nicola Talbot
NinV
Posts: 69
Joined: Sun Oct 05, 2008 12:57 pm

Conditional IF with characters (INDEXING)

Post by NinV »

nlct wrote:You could define a command like:

Code: Select all

\newcommand*{\word}[1]{-#1\index{#1@-#1}}
then you can do \word{sample} which would display -sample in the text and index -sample but sort it without the - symbol.
That's what I'm doing now. But I would like to have a command like:

Code: Select all

\newcommand*{\word}[1]{
%\if \word starts with a symbol (-)
-#1\index{#1@-#1}
%\else
#1\index{#1}
%\fi
}
nlct wrote: Alternatively, there are packages like datatool that can check for a given prefix.
I've had a look to the package you suggest and I think that its command: \DTLifStartsWith can be what I'm looking for!

Thank you very much for your help.

Ciao,
Nino
Window + MiKTeX 2.7
Ubuntu + TexLive + Kile
Xandros (eeePC) + TeTex + TexMaker
NinV
Posts: 69
Joined: Sun Oct 05, 2008 12:57 pm

Conditional IF with characters (INDEXING)

Post by NinV »

Hi,

I'm trying to use datatool as suggested, but if I use the command

Code: Select all

\DTLsplitstring{-andika}{-}{\before}{\after}
directly in a text it works fine, but if I use it in a new command:

Code: Select all

\newcommand\term[1]{
\DTLsplitstring{#1}{-}{\before}{\after}
\ifx\after\#1
  #1\index{#1}
\else
  #1\index{\after@#1}
\fi
}
I get the following error message:

Code: Select all

!Undefined control sequence.
\DTLsplitstring #1#2#3#4-> \def \dtl@splitstr
                                             ##1#2##2\@nil {\de #3{##1}\def...
Can you help to understand what's happening?

Thanks a lot,
Nino
Window + MiKTeX 2.7
Ubuntu + TexLive + Kile
Xandros (eeePC) + TeTex + TexMaker
User avatar
nlct
Posts: 276
Joined: Thu Nov 06, 2008 11:15 am

Conditional IF with characters (INDEXING)

Post by nlct »

I don't get an error with the latest version (2.0) but your code has a couple of problems:

Code: Select all

\ifx\after\#1
This is comparing \after with \# and if true prints a "1".

If the symbol isn't present \after is empty and \before contains the original string, so you need to compare \before instead:

Code: Select all

\newcommand\term[1]{%
\def\Term{#1}%
\DTLsplitstring{#1}{-}{\before}{\after}
\ifx\before\Term
  #1\index{#1}
\else
  #1\index{\after @#1}
\fi
}
Hope that helps.

Regards
Nicola Talbot
NinV
Posts: 69
Joined: Sun Oct 05, 2008 12:57 pm

Conditional IF with characters (INDEXING)

Post by NinV »

Hi Nicola,

sorry for bothering you but even correcting the code as suggested it does work for me :?

I post a simplified version of my code, even so it does work!

I have three files. The main one is the following:

Code: Select all

\documentclass[11pt,twoside,a4paper]{book}
\usepackage[latin1]{inputenc}
\usepackage[esperanto, italian]{babel}
\usepackage{datatool}
\usepackage{fancyhdr}
\pagestyle{fancy}

\input{preambolo/talbndx}

\includeonly{%
capitoli/talbot01
}

\begin{document}

\pagestyle{fancy}
\include{capitoli/talbot01}
\end{document}
It calls some parts for the preamble, i.e. the redifinition of index commands:

Code: Select all

%
% index
%
\newcommand\term[1]{
\def\Term{#1}%
\DTLsplitstring{#1}{-}{\prima}{\dopo}%
\ifx\prima\Term % senza trattino
#1\index{#1}
\else % con trattino
#1\index{\dopo @#1}
\fi
}
and the text chapters:

Code: Select all

%********** Capitolo 1 **********
\setcounter{chapter}{1}
\setcounter{section}{0}
\chapter{Somo la kwanza}

\section{Maelezo (\textit{Spiegazioni})}

\subsection{\term{Forme di saluto} (I)}

Il saluto più usato (soprattutto con europei)
If I compile the main file I get the said error!

Would you like to try?

Thank you very much for your help.

Nino
Window + MiKTeX 2.7
Ubuntu + TexLive + Kile
Xandros (eeePC) + TeTex + TexMaker
NinV
Posts: 69
Joined: Sun Oct 05, 2008 12:57 pm

Conditional IF with characters (INDEXING)

Post by NinV »

Hi,

I think that the problem is \subsection{}, because I changed

Code: Select all

\subsection{\term{Forme di saluto} (I)}
Il saluto più usato (soprattutto con europei)
in

Code: Select all

\subsection{Forme di saluto (I)}
Il \term{saluto} più usato (soprattutto con europei)
Il \term{-saluto} più usato (soprattutto con europei)
and the code works well... but I need to use it everywhere :(

Another problem is that \DTLsplitstring{} doesn't ignore commands like \textit, so I can't use it this way:

Code: Select all

\term{\textit{saluto}}
Any hint?

Thanks a lot.

Nino
Window + MiKTeX 2.7
Ubuntu + TexLive + Kile
Xandros (eeePC) + TeTex + TexMaker
User avatar
nlct
Posts: 276
Joined: Thu Nov 06, 2008 11:15 am

Conditional IF with characters (INDEXING)

Post by nlct »

\subsection (and all the other sectioning commands) has a moving argument so you either need to protect \term (using \protect\term) or you need to make \term robust (define it using \DeclareRobustCommand instead of \newcommand) or you need to use the optional argument:

Code: Select all

\subsection[Forme di saluto (I)]{\term{Forme di saluto} (I)}
For terms that need to have a font change, you could set the font change as an optional argument, for example:

Code: Select all

\DeclareRobustCommand\term[2][]{%
\def\Term{#2}%
\DTLsplitstring{#2}{-}{\before}{\after}
\ifx\before\Term
  #1{#2}\index{#2}
\else
  #1{#2}\index{\after @#2}
\fi
}
then you can do, e.g.

Code: Select all

\term[\textit]{word}
Hope that helps.

Regards
Nicola Talbot
NinV
Posts: 69
Joined: Sun Oct 05, 2008 12:57 pm

Re: Conditional IF with characters (INDEXING)

Post by NinV »

It works!

Thanks a lot for your kindness.

I'm a beginner and I find the LaTeX guides, which I'm studying hard, very unfriendly!

I knew very little about \DeclareRobustCommand :oops:

Thank you very much,
Nino
Window + MiKTeX 2.7
Ubuntu + TexLive + Kile
Xandros (eeePC) + TeTex + TexMaker
Post Reply