3

I want to have easier ways to index large texts such as thesis, reports, books. I found lots of posts here asking for some magic tool to do that automatically. And as far as I could realize from the comments no one believe a machine can do such an important TASK for us.

So, I think the best solution to facilitate our indexing work is to create proper personal commands (which I am not good at so far...). I want to do at least the following:

  1. Have a way to print the indexed word in the text, i.e. to avoid typing things twice

    (some text) word \index{word} ...
    

    with the definition of command \self in the way below I get what I want.

    \DeclareRobustCommand{\self}[1]{{#1}\index{#1}} %solves 1 
    
  2. Define command for entries inside a indexed word repeated multiple times. ex.: avoid repeating "word"

    \index{word! entry1}, \index{word! entry1}, \index{word! entry1}...
    

The command \sub below makes it.

    \DeclareRobustCommand{\sub}[1]{\texttt{#1}\index{Set subordinate!#1}} %solves2
  1. Same of 2 but now the command must use a variable define in parts of text (to replace the fixed "word" in the previous). Is that possible? see attempt on section "Especial" inside the example below

  2. Command to split index (\splex) entries (n variable) in multiples \index{}? To avoid makeindex to print all entries in the same line when I use \index{Cramer, determinant, matrices} for example. I have no idea how to start to write the command, especialy because I am assuming a general case where the number of entries varies. My idea is to use \splex{Cramer, determinant, matrices} in order to produce \index{Cramer} \index{determinant} \index{matrices} in the text..

I found some similar commands on other examples on internet and I am adjusting for my interest but some I still can't figure it out. Are the commands inside the example below ok, or could they be improved/replaced? And how to solve point 3 and 4?

\documentclass{article}
\usepackage{makeidx}
\makeindex
%\DeclareRobustCommand{\self}[1]{\texttt{#1}\index{#1}} %solves 1 but changes text style
\DeclareRobustCommand{\self}[1]{{#1}\index{#1}} %solves 1 
\DeclareRobustCommand{\sub}[1]{\texttt{#1}\index{Set subordinate!#1}} %solves2
\newcommand{\subdef}[2]{\index{\subd!#1}} %attempt to 3
\begin{document}
  Usefull \self{commands}.

   (some text) \sub{entry1}. 
   (some text)  \sub{entry2}. 

    \newpage
    \section{Especial *NOT WORKING*}
    \def\subd{especial}
     Something1  \subdef{entry1}. 
     Something2  \subdef{entry2}. 
\printindex
\end{document}

Addendum

If you have a good tutorial to indicate for learning to create personal commands (from basic to complex commands) please post the link on the comments.

7

I'm not entirely sure I understand what point 3 is getting at. Your command specifies 2 arguments but you only use one in the definition and you don't have any code there which would put the argument in the text, which I thought was desired in this case. However, as best I understand 3, this solves it, I think.

4 is more straightforward to understand in terms of your desiderata. However, my solution is a little more complex since it relies on etoolbox.

\documentclass{article}
\usepackage{makeidx,etoolbox}
\makeindex
\DeclareRobustCommand{\self}[1]{{#1}\index{#1}}% solves 1
\DeclareRobustCommand{\sub}[1]{\texttt{#1}\index{Set subordinate!#1}}% solves2
\DeclareRobustCommand{\subdef}[1]{\texttt{#1}\index{\subd!#1}}% solves 3? (not sure I've understood the aim of 3)
% etoolbox manual p. 24
\newcommand\splex[1]{% solves 4
  \forcsvlist{\listadd\mylist}{#1}%
  \forlistloop{\index}{\mylist}}
\begin{document}
  Useful \self{commands}.

  (some text) \sub{entry1}.
  (some text)  \sub{entry2}.

  \section{Especial *WORKING*}
  \def\subd{especial}
  Something1  \subdef{entry1}.
  Something2  \subdef{entry2}.

  \section{Another test}
  \def\subd{another test}
  Something 3  \subdef{entry3}.
  Something 4 \subdef{entry4}.

  \section{Split commands}
  Nothing much to say.\splex{Cramer, determinant, matrices}

  \printindex
\end{document}

text and list

  • Yes, you've got my point on 3. I edited my post to let it more clear. And about 4 that was my intention also, but I had no ideia how to start building such a command. I'll accept the answer. Another question that arose is if \DeclareRobustCommand could be replaced by \newcommand. Ty – Claudia Dec 22 '14 at 11:51
  • @Claudia Why not try it and see? etoolbox is very useful - definitely worth a look. It has a lot of facilities for handling lists, patching macros etc. – cfr Dec 22 '14 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.