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

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}


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

• The fact that the content is "going off the right side of the page" is because of the odd final entry in the last row. It's probably meant to go into the second column, not the first. So precede it with a &... Jul 28 at 16:47

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>}


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

• Thanks a lot! Really bizarre, but a neat solution :) Jul 28 at 21:18

In some places you put your \index{} in the wrong column. Also, do not start a column entry with \index{} put it after a word. See below for a revised version of your tabular which is now narrow enough to fit in the textblock.

 % indexprob.tex  SE 606820

\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}
%%%%%%%%%%  PW changes to tabular
\begin{tabular}{rp{0.385\linewidth}p{0.385\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}
%%%%%%%%%%%% end PW changes

\printindex
\end{document}


• Thanks a lot, this is great! Jul 28 at 21:19
• Would really look nicer with the data in the cells set \raggedright. Jul 29 at 0:08

The problem here is that by default LaTeX treats a tabular environment as if it were just a letter (in technical terms, it typesets it in horizontal mode).¹ You can see this even more dramatically by slightly modifying your example to read:

Some words
\begin{tabular}{rp{0.3\linewidth}p{0.3\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}
more words


(I also narrowed the columns a bit ❶ to make it more obvious what's happening.

In my LaTeX book, before introducing tabular, I recommend that users create a new environment displaytbl to put around their tabular environments. One possible definition is:

\NewDocumentEnvironment{displaytbl}{}
{\begin{center}}
{\end{center}}


This allows you to consistently control the formatting of tabulars in the document. You might add \sffamily to the \begin definition, for example, to have all tabulars to be set in sans-serif type. A sufficiently clever version of the environment could also redefine \footnote in such a way to not require the separation of \footnotemark and \footnotetext as is normally required in a tabular context.

So my suggestion is to add the defintion of displaytbl to your document and then change your timeline output to be:

\begin{displaytbl}
\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}
\end{displaytbl}


1. This is arguably a defect in LaTeX but it's long past the time for it to be corrected. There are rare instances when you would want this behavior (e.g., putting a tabular inside another tabular), but in most cases it's more an inconvenience than a help.
• This was an interesting read and probably the easier solution, but I'm going with the others' one. I don't know why someone downvoted you :( But thanks a lot! Jul 28 at 21:21