0

Some index references are shown in the next line if there are too many sections with the same \index. This is fine, but I want to align it on the right-hand side (as shown in the picture below). How can I do this? I guess I should replace the \hangindent 2em in \renewcommand*{\@idxitem}{\par\nopagebreak\hangindent 2em\hskip 0.8cm} but I have no idea how to do this.

enter image description here

\documentclass{scrbook}

\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[linktoc=none]{hyperref}
\usepackage{imakeidx}

\renewcommand\thesection{\arabic{section}} 

\RequirePackage{filecontents}
\begin{filecontents*}{Index.ist}
headings_flag 1
heading_prefix "\\indexheading{"
heading_suffix "}"

delim_0 "\\hfill"

group_skip "\n"
preamble "\\begin{theindex}\\starttheindex"
postamble "\n\\stoptheindex\\end{theindex}"
\end{filecontents*}

\usepackage{imakeidx}
\makeindex[options=-s Index.ist, columnsep=5mm] 
\LetLtxMacro\OldIndex\index
\newcommand*{\indexpage}[5]{#2\ifnum#4=0 \else.#4\fi}
\renewcommand{\index}[1]{\OldIndex{#1|%
   indexpage
    {\ifnum\value{section}<10 0\fi}{\arabic{section}}%
    {\ifnum\value{subsection}<10 0\fi}{\arabic{subsection}}}%
} 
\newcommand*{\indexheading}[1]{%
  \indexrule
  \hspace*{1mm}\textbf{#1}\par
  \nopagebreak
  \vskip-\baselineskip
}
\newcommand*{\indexrule}{%
  \par\nopagebreak
  \vskip\dp\strutbox\nopagebreak
  \hrulefill\par\pagebreak[3]
  \vskip -\baselineskip
  \hrulefill\par\nobreak
}
\newcommand*{\starttheindex}{\raggedcolumns}
\newcommand*{\stoptheindex}{\indexrule}
\makeatletter
\renewcommand*{\@idxitem}{\par\nopagebreak\hangindent 2em\hskip 0.8cm} 
\makeatother

\begin{document}
\section{English football league system}
\index{System}
%
\subsection{History}
Created in 1888.
%
\subsection{Structure}
\index{Structure}
At the top is the single division of the Premier League
%
\subsection{Cup eligibility}
\index{Cup}
Being members of a league at a particular level also affects eligibility for Cup.
%
\subsection{Sub 4}
\index{Cup}
%
\subsection{Sub 5}
\index{Cup}
%
\subsection{Sub 6}
\index{Cup}
%
\subsection{Sub 7}
\index{Cup}
%
\subsection{Sub 8}
\index{Cup}
%
\subsection{Sub 9}
\index{Cup}
%
\subsection{Carabao Cup}
\index{Cup}

\printindex
\end{document}
1

I could solve the problem using \raggedright and \raggedleft.

\documentclass{scrbook}

\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[linktoc=none]{hyperref}
\usepackage{imakeidx}

\renewcommand\thesection{\arabic{section}} 

\RequirePackage{filecontents}
\begin{filecontents*}{Index.ist}
headings_flag 1
heading_prefix "\\indexheading{"
heading_suffix "}"

delim_0 "\\hfill"

group_skip "\n"
preamble "\\begin{theindex}\\starttheindex"
postamble "\n\\stoptheindex\\end{theindex}"
\end{filecontents*}

\usepackage{imakeidx}
\makeindex[options=-s Index.ist, columnsep=5mm] 
\LetLtxMacro\OldIndex\index
\newcommand*{\indexpage}[5]{#2\ifnum#4=0 \else.#4\fi}
\renewcommand{\index}[1]{\OldIndex{#1|%
   indexpage
    {\ifnum\value{section}<10 0\fi}{\arabic{section}}%
    {\ifnum\value{subsection}<10 0\fi}{\arabic{subsection}}}%
} 
\newcommand*{\indexheading}[1]{%
  \indexrule
  \raggedright\hspace*{1mm}\textbf{#1}\par % NEW CODE (with\raggedright)
  \nopagebreak
  \vskip-\baselineskip
}
\newcommand*{\indexrule}{%
  \par\nopagebreak
  \vskip\dp\strutbox\nopagebreak
  \hrulefill\par\pagebreak[3]
  \vskip -\baselineskip
  \hrulefill\par\nobreak
}
\newcommand*{\starttheindex}{\raggedcolumns}
\newcommand*{\stoptheindex}{\indexrule}
\makeatletter 
\renewcommand*{\@idxitem}{\par\nopagebreak\hangindent 2em\raggedleft\hskip 0.8cm}  % NEW CODE (with \raggedleft)
\makeatother

\begin{document}
\section{English football league system}
\index{System}
%
\subsection{History}
Created in 1888.
%
\subsection{Structure}
\index{Structure}
At the top is the single division of the Premier League
%
\subsection{Cup eligibility}
\index{Cup}
Being members of a league at a particular level also affects eligibility for Cup.
%
\subsection{Sub 4}
\index{Cup}
%
\subsection{Sub 5}
\index{Cup}
%
\subsection{Sub 6}
\index{Cup}
%
\subsection{Sub 7}
\index{Cup}
%
\subsection{Sub 8}
\index{Cup}
%
\subsection{Sub 9}
\index{Cup}
%
\subsection{Carabao Cup}
\index{Cup}

\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.