1

I'm using indexing with the doc package and I've run in to an issue. When I have keys that are more than one word they justify over the whole column. I wanted to use the \efill macro to get the keys left aligned but keep the numbers right aligned. If I use the \efill inside the key the , is moved to the right.

I tried reading the package documentation but I can't figure out how to do it.

How can I set it up so that the \efill is to the left of the , or replace the , with the \efill?

MWE

\documentclass[a4paper, 11pt]{report}

\usepackage[utf8]{inputenc} 
\usepackage[english]{babel} % English language/hyphenation
\usepackage{hyperref}
\usepackage{multicol}
\usepackage{doc}

\IndexPrologue{\chapter*{Index}\markboth{Index}{Index}}% section heading, no message

\usepackage{makeidx}
\makeindex

\begin{document}

Some text \index{Spread out} \index{\textbackslash efill\efill}

\addcontentsline{toc}{chapter}{Index}
\printindex

\end{document}

enter image description here

  • A rather naïve (and possibly buggy) solution would be to define \def\argfill#1{#1\efill} then use \argfill instead of \efill. – Phelype Oleinik Jan 24 at 19:13
  • Wouldn't that look exactly the same as when I use \efill above? – Kajsa Jan 24 at 19:45
  • 1
    No, because the command \argfill will take one argument, which will be the comma. The command then places the comma (#1) and then the \efill command, making the swap you asked. I posted a few alternatives for you. – Phelype Oleinik Jan 24 at 20:17
2

That comma is inserted automatically by makeindex, so you can't (in an elegant way) put an \efill (which, for the record, is \hfill\nopagebreak) after the comma in the TeX side of the thing.

I'll give you a few possible solutions, depending on exactly how you want to achieve that.

If you want that in all entries (correct way):

You have to create a custom style file for makeindex which automagically prints the \efill after the comma. A simple style file which does that is:

delim_0    ",\\myfill "
delim_1    ",\\myfill "
delim_2    ",\\myfill "

(I used \myfill instead of \efill so that you can redefine that easily later).

Save that to a file called, say, flushcomma.ist and, suppose your .tex file is called mydoc.tex, compile your document then run makeindex with:

makeindex -s flushcomma.ist -o mydoc.ind mydoc.idx

If you are not a command line person, you can use Arara. Put these lines at the top of your mydoc.tex:

% arara: pdflatex
% arara: makeindex: { style: flushcomma.ist }
% arara: pdflatex

and define the \myfill command:

\def\myfill{\hfill\nopagebreak}

Full MWE:

% arara: pdflatex
% arara: makeindex: { style: flushcomma.ist }
% arara: pdflatex
\documentclass{report}
\usepackage{doc}
\IndexPrologue{\chapter*{Index}\markboth{Index}{Index}}
\usepackage{makeidx}
\makeindex
\def\myfill{\hfill\nopagebreak}
\begin{document}
Some text \index{Spread out} \index{\textbackslash efill}
\addcontentsline{toc}{chapter}{Index}
\printindex
\end{document}

If you want that in all entries (questionable way):

Then you have to somehow change the macro that writes the contents of the \index command to the .idx file. If you don't have hyperref loaded you can put this definition in your preamble:

\def\@wrindex#1{%
   \protected@write\@indexfile{}%
      {\string\indexentry{#1a|myfill}{\thepage}}%
 \endgroup
 \@esphack}%

if you load hyperref, however, then you can use this one:

\def\@wrindex#1{\@@wrindex#1|myfill|\\}%

Full MWE:

% arara: pdflatex
% arara: makeindex
% arara: pdflatex
\documentclass{report}
\usepackage{doc}
\IndexPrologue{\chapter*{Index}\markboth{Index}{Index}}
\usepackage{makeidx}

%% Without hyperref
% \makeatletter
% \def\@wrindex#1{%
%    \protected@write\@indexfile{}%
%       {\string\indexentry{#1a|myfill}{\thepage}}%
%  \endgroup
%  \@esphack}%
% \makeatother
%%%%%%%%%%%%%%%%%%%

%% With hyperref
\usepackage{hyperref}
\makeatletter
\def\@wrindex#1{\@@wrindex#1|myfill|\\}%
\makeatother
%%%%%%%%%%%%%%%%

\makeindex
\def\myfill{\hfill\nopagebreak}
\begin{document}
Some text \index{Spread out} \index{\textbackslash efill}
\addcontentsline{toc}{chapter}{Index}
\printindex
\end{document}

If you want that in only a few entries:

Then, as the very next thing that the makeindex program writes after the index entry is the , then you can define the \myfill command like this:

\def\myfill#1{#1\hfill\nopagebreak}

the command will take an argument, the comma, and will put it behind the \hfill in the entries where you use it. Make sure that it is the very last thing you pass to the \index command, or it won't work.

Full MWE:

% arara: pdflatex
% arara: makeindex
% arara: pdflatex
\documentclass{report}
\usepackage{doc}
\IndexPrologue{\chapter*{Index}\markboth{Index}{Index}}
\usepackage{makeidx}
\makeindex
\def\myfill#1{#1\hfill\nopagebreak}
\begin{document}
Some text \index{Spread out} \index{\textbackslash efill\myfill}
\addcontentsline{toc}{chapter}{Index}
\printindex
\end{document}

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.