Imakeidx package making table lose alignment when \index{} is used

indexingtablesvertical alignment

My table was aligned very nicely, until I started trying to add indices to my book. The index at the end is loading fine, but the tables where I used the \index method inside of lose their vertical alignment. Can anyone help? Below is example code and result

\documentclass{book}
\usepackage[paperwidth=6in, paperheight=9in, margin=0.8in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{titlesec}
\usepackage{etoolbox}
\usepackage{imakeidx}
\makeindex
\makeatletter
\patchcmd\chapter{\thispagestyle}{\global\c@footnote\z@\thispagestyle}{}{}
\makeatother
\titleformat{\chapter}{\normalfont\huge}{\thechapter.}{20pt}{\huge\it}
\renewcommand{\chaptermark}[1]{\markboth{}{\sffamily #1}}
\begin{document}
\frontmatter
\maketitle
\tableofcontents
\mainmatter
\chapter{Timeline of astronomers and astrologers}
\begin{tabular}{rp{0.4\linewidth}p{0.4\linewidth}}
    \textbf{Year} & \textbf{Original text} & \textbf{Translation} \\
    1990BC & \index{Zoroastres}Zoroastres Bactrianorum Rex & \index{Zoroaster}Zoroaster, king of Bactria \footnotemark \\
    1590BC & \index{Prometheus}Prometheus Atlantis frater & Prometheus, brother of Atlas \\
    1580BC & \index{Atlas}Atlas Rex Mauritaniae & Atlas, King of Mauretania \footnotemark \\\index{Mercury}Mercury the greater \\
\end{tabular}
\printindex
\end{document}

Result image

Before I added the \index commands, the lines were vertically aligned and not going off the right side of the page.

Best Answer

Starting a paragraph (in the second/third column) with an \index (which technically doesn't set anything) is the cause of this issue. Instead of using

\index{<name>}<name>

consider using

<name>\index{<name>}

enter image description here

\documentclass{book}

\usepackage[paperwidth=6in, paperheight=9in, margin=0.8in]{geometry}
\usepackage{imakeidx}
\makeindex

\begin{document}

\chapter{Timeline of astronomers and astrologers}

\begin{tabular}{ r p{0.4\linewidth} p{0.4\linewidth} }
  \textbf{Year} & \textbf{Original text} & \textbf{Translation} \\
         1990BC & Zoroastres\index{Zoroastres} Bactrianorum Rex & Zoroaster\index{Zoroaster}, king of Bactria \footnotemark \\
         1590BC & Prometheus\index{Prometheus} Atlantis frater  & Prometheus, brother of Atlas                              \\
         1580BC & Atlas\index{Atlas} Rex Mauritaniae            & Atlas, King of Mauretania \footnotemark                   \\
                & Mercury\index{Mercury} the greater \\
\end{tabular}

\printindex

\end{document}

This shift in \index is not an issue in this setup as the tabular will keep the content on the same page - necessary when thinking about indices pointing to a specific page. If you had content starting a paragraph that could span a page boundary, then use

\leavevmode\index{<index>}<index> ...

instead. \leavevmode (or just \mbox{}) will initiate horizontal mode so the paragraph can start.

Related Question