6

I have this document structure:

\documentclass[11pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[margin=1in]{geometry}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{etoc}
%\usepackage{hyperref}

\etocsettocstyle{}{}
% Section entries: no number, normal font, dotted leaders
\etocsetstyle{section}{}{}{\noindent\normalfont\etocname\dotfill\etocpage\par}{}
\setcounter{tocdepth}{1}

\begin{document}
 % no title, no space before/after ToC

\begin{tabularx}{\textwidth}{X|X}
\toprule
Author & Luke Skywalker \\
\midrule
Company & Star Wars Inc. \\
\midrule
Role & Jedi Knight \\
\midrule
Contents & \tableofcontents \\
\bottomrule
\end{tabularx}


\section{Section Title 1}
Section 1 content goes here.

\section{Section Title 2}
Section 2 content goes here.

\section{Section Title 3}
Section 3 content goes here.

\end{document}

That produce this look that i like: enter image description here

However, if i want the table of content to be clickable, i can uncomment \usepackage{hyperref}. Unfortunately, doing so will disalign the toc with the other column: enter image description here

How can i avoid hyperref to interact with the line spacing (At least in the context of the toc)?

7
  • 3
    \vspace*{-\topskip}\tableofcontents. Of course you shouldn't be using vertical rules along with booktabs horizontal rules. Commented Sep 10 at 13:50
  • thanks for the quick reply! Fortunately, i did not plan to use booktabs in my actual project. However, can you clarufy why i would need to avoid using booktabs and hyperref together in the future? Thanks in advance! Commented Sep 10 at 14:06
  • 2
    The rules don't connect and the gaps are terrible. Commented Sep 10 at 14:11
  • also, i noticed that, despite being better, also in my first example the right column is not aligned. This happens only for the toc, "Jedi" is perfectly aligned, but "Section Title 1" is not Commented Sep 10 at 14:16
  • Welcome to TeX.SX! Unrelated, \usepackage[utf8]{inputenc} is now useless. Commented Sep 10 at 15:18

2 Answers 2

5

As a workaround, you can replace

\tableofcontents

with

\noindent\etocinline\localtableofcontents

Quoting the etoc manual (section 9.1.2, The \etocinline and \etocdisplay commands)

With \etocinline, or its synonym \etocnopar, the \localtableofcontents command and its variants do not first issue a \par to close the previous paragraph.

Example:

\documentclass[11pt,a4paper]{article}

\usepackage[T1]{fontenc}
\usepackage[margin=1in]{geometry}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{etoc}
\usepackage{hyperref}

\etocsettocstyle{}{}

% Section entries: no number, normal font, dotted leaders
\etocsetstyle{section}{}{}{\noindent\normalfont\etocname\dotfill\etocpage\par}{}
\setcounter{tocdepth}{1}

\begin{document}
 % no title, no space before/after ToC
\begin{tabularx}{\textwidth}{X|X}
\toprule
Author & Luke Skywalker \\
\midrule
Company & Star Wars Inc. \\
\midrule
Role & Jedi Knight \\
\midrule
Contents & \noindent\etocinline\localtableofcontents \\
\bottomrule
\end{tabularx}


\section{Section Title 1}
Section 1 content goes here.

\section{Section Title 2}
Section 2 content goes here.

\section{Section Title 3}
Section 3 content goes here.

\end{document}

Example

4
  • 1
    +1 it also works in your mwe with \tableofcontents no need for \localtableofcontents. The explanation though is not that the \par caused extra vertical space, but it is the one explained by @ufischer about hyperref tardet. Thanks to your \noindent the target is emitted while already in horizontal mode and no extra space gets added. But then \etocinline is mandatory to avoid creating an empty paragraph initiated by \noindent. Commented Sep 11 at 11:39
  • If you add \par A\par before the \noindent you will see the problem was not the \par but the \refstepcounter before a paragraph start. Commented Sep 11 at 11:41
  • @user691586 Yes, curiously \etocinline works also with \tableofcontents, even if the manual doesn't say it. This answer is just a workaround, I agree the real issue is the one found by Ulrike Fischer. This should probably be reported to the etoc developer as the manual claims a full compatibility with hyperrref. Commented Sep 11 at 20:15
  • 1
    I am not sure there is an "incompatibility with hyperref". The issue is with \refstepcounter{} done while in vertical mode. I don't think LaTeX manuals explicitly say never to do that. The issue is due to modified \refstepcounter causing in some circumstances such as p-table cells extra white space if at start. I would say that etoc reveals such difficulty with hyperref. From what I understand it is is needed for etoc to do this to support features of the package. The clever work-around you found could probably be forwarded to etoc maintainer with a query to include it in doc Commented Sep 11 at 20:39
6

etoc issues a \refstepcounter at the begin and with hyperref this also creates a link target. Probably etoc could either disable the link or simply use \stepcounter, but the author will know more about it.

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[margin=1in]{geometry}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{etoc}
\usepackage{hyperref}

\etocsettocstyle{}{}
% Section entries: no number, normal font, dotted leaders
\etocsetstyle{section}{}{}{\noindent\normalfont\etocname\dotfill\etocpage\par}{}
\setcounter{tocdepth}{1}
\makeatletter
\def\Etoc@table@fcontents{%
    \LinkTargetOff\refstepcounter{etoc@tocid}\LinkTargetOn %or even \stepcounter{etoc@tocid}
    %
    \Etoc@tocwithidfalse
    \futurelet\Etoc@nexttoken\Etoc@t@bleofcontents
}
\makeatother
\begin{document}
 % no title, no space before/after ToC
\begin{tabularx}{\textwidth}{X|X}
\toprule
Author & Luke Skywalker \\
\midrule
Company & Star Wars Inc. \\
\midrule
Role & Jedi Knight \\
\midrule
Contents & \tableofcontents \\
\bottomrule
\end{tabularx}



\section{Section Title 1}
Section 1 content goes here.

\section{Section Title 2}
Section 2 content goes here.

\section{Section Title 3}
Section 3 content goes here.

\end{document}
8
  • Thanks! It works perfectly! Commented Sep 10 at 14:29
  • or the OP could have used the other answer to their previous question, which wouldn't have introduced etoc ;-) Commented Sep 10 at 15:50
  • thanks @DavidCarlisle, sorry that I did not see your previous anwer in time and proceeded with adding `etoc. I link your previous answer here since it is very related: link Commented Sep 11 at 6:19
  • After (too much) time trying to understand doc, I gather that it is a package feature to be able to say "See the global TOC which appears on page \pageref{mytoc}" and elsewhere in document there is a \tableofcontents\label{mytoc}. (I am deleting my earlier comment now that I (think to) understand better). But only maintainer could say, the etoc code looks like a maze to me. Commented Sep 11 at 8:26
  • @user691586 a pageref to a label after \tableofcontents will give problems if the toc has more than one page. Commented Sep 11 at 8:39

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.