toc links point to wrong pages - latex

i have a simple tex file. when i compile it with $ pdflatex test.tex it generates a pdf. the table of contents has clickable links but they don't work!
the page numbering is correct, but when i click a line it directs me to the wrong page.
has anyone an idea how i can fix it?
here is my code:
\documentclass[12pt,titlepage]{scrartcl}
\usepackage[nottoc,numbib]{tocbibind}
\usepackage{graphicx}
\usepackage[section]{placeins}
\usepackage{float}
\usepackage{amsmath}
\usepackage[autostyle=false,german=quotes]{csquotes} %% for \enquote{}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage[]{hyperref}
\title{test}
\author{me}
\date{\today}
\begin{document}
\pagenumbering{roman}
\begin{titlepage}
\maketitle
\end{titlepage}
\setcounter{section}{-1}
\section[]{Abstract}
blabla bla
\newpage
\tableofcontents
\newpage
\pagenumbering{arabic}
\part{part1}
\setcounter{section}{-1}
\section[]{Abstract}
\section{sec a 1}
bla bla
\subsection{sec a 1.1}
bla blub
\section{sec a 2}
\newpage
\part{part2}
\setcounter{section}{-1}
\section[]{Abstract}
\section{sec b 1} % the toc entry for this section points to page 1 but it is on page 2
blub blub
\section{sec b 2} % the toc entry for this section points to page 1 but it is on page 2
blub blub
\subsection{sec b 2.1} % the subsection in the toc points to the right 2nd page!
\end{document}
and here you see the toc. as you can see, the mouse over hint tells that the link points to page 1 but the number on the right says it is on page 2.
thanks in advance.

Whenever you reuse a hyperref counter that could make its way into a link, duplicate hyperlinks occur. Visually this is fine, but internal to the document, the jumps are ambiguous. So, you need to provide hyperref some help.
The easiest way to circumvent this problem and provide hyperref some assistance is to add to your preamble
\renewcommand{\theHsection}{\thepart.section.\thesection}
after loading hyperref. The above prepends \thepart. to the hyperref-related \section link. Since the re-use of \section counters is based on your use of \parts, this addition will make for a unique link and remove ambiguous destinations.

Related

Paragraph page number misalignment in table of contents

I am using the Ph.D. dissertation .cls file. When I create a paragraph there is a misalignment in the paragraph page number in TOC.
Below is the minimal code:
\documentclass[final]{USC-Thesis_Minimal}
\usepackage[lofdepth,lotdepth,caption=false]{subfig}
\begin{document}
\title{Some title}
\author{Some Name}
\major{Some Engineering}
\month{May}
\year{2019}
\setcounter{secnumdepth}{5}
\setcounter{tocdepth}{5}
\maketitle
% Table Of Contents
\cleardoublepage\phantomsection
\renewcommand{\contentsname}{Table of Contents}
\tableofcontents
% List of tables
\listoftables
% List of figures
\listoffigures
% Begin Body
\mainmatter
\chapter{Chapter 1}
\section{A very long section, very long text section}
\subsection{A very long subsection, very long text subsection}
\subsubsection{A very long subsubsection Very long text subsubsection}
\paragraph{A long paragraph}
\end{document}
Below is the relevant part of the cls file.
%Margins of Table of Contents - so that the page listings for toc do
%not align with page number
\cftsetrmarg{0.7in}
\newlength{\numtomarg}
\setlength{\numtomarg}{0.2in}
\renewcommand{\cftchapafterpnum}{\hspace{\numtomarg}\mbox{}}
\renewcommand{\cftsecafterpnum}{\hspace{\numtomarg}\mbox{}}
\renewcommand{\cftsubsecafterpnum}{\hspace{\numtomarg}\mbox{}}
\renewcommand{\cftsubsubsecafterpnum}{\hspace{\numtomarg}\mbox{}}
\renewcommand{\cftfigafterpnum}{\hspace{\numtomarg}\mbox{}}
\renewcommand{\cftsubfigafterpnum}{\hspace{\numtomarg}\mbox{}}
\renewcommand{\cfttabafterpnum}{\hspace{\numtomarg}\mbox{}}
\renewcommand{\cftsubtabafterpnum}{\hspace{\numtomarg}\mbox{}}
I couldn't find an alternative for the paragraph. Similar to:
\renewcommand{\cftparagraphafterpnum}{\hspace{\numtomarg}\mbox{}}
Here is the link to the class file. Can somebody please help me to fix this issue?
Using the below LaTeX command did the trick:
\renewcommand{\cftparaafterpnum}{\hspace{\numtomarg}\mbox{}}

part lettering Latex

I am trying to get my document parts to show up as:
A. Narrative % \part{Narrative}
Intro %\section{Intro}
main text...
B. Appendix % \part{Appendix}
Derivations % \section{Derivations}
appendix text...
I have seen others use:
\renewcommand{\thepart}{\Alph{part}}
However this is not working for me for some reason. My parts are showing up as:
Part A
Narrative
Intro
main text...
Part B
Appendix
Derivations
appendix text...
Any ideas anyone?
The minimal example below updates \part to set its numbering differently. More specifically, it removes the \partname - Part - prefix and keeps the title on the same line. Fonts are also updated to set the part using \LARGE\bfseries in both \part and \part*. All of the above updates are done using etoolbox's \patchcmd{<cmd>}{<search>}{<replace>}{<success>}{<failure>} macro that performs a <search>-and-<replace> within <cmd>.
\documentclass{article}
\usepackage{lipsum,etoolbox}
\renewcommand{\thepart}{\Alph{part}}
\makeatletter
% Change part display; also uniform size of \LARGE\bfseries
\patchcmd{\#part}% <cmd>
{\Large\bfseries \partname\nobreakspace\thepart \par\nobreak}% <search>
{\LARGE\bfseries \thepart.\quad}% <replace>
{}{}% <success><failure>
\patchcmd{\#part}{\huge}{\LARGE}{}{}
\patchcmd{\#spart}{\huge}{\LARGE}{}{}
\renewcommand{\#seccntformat}[1]{\csname the#1\endcsname.\quad}
% \#addtoreset{section}{part} % Reset section counter with every part
\makeatother
\begin{document}
\part{Narrative}
\section{Intro}
\lipsum[1]
\part{Appendix}
\section{Derivations}
\lipsum[2]
\end{document}
If you wish to have the \section numbers reset with every new \part, uncomment the line referencing that in the preamble.
Your idea is right, but you also redefine the titleformat.
From the following link:
\usepackage{titlesec}
\renewcommand{\thepart}{\Alph{part}}
\makeatletter
\titleformat{\part}[display]
{\Huge\scshape\filright}
{\thepart~\partname}
{20pt}
{\thispagestyle{plain}}
\makeatother

LaTeX hyperref link goes to wrong page when i clicked at the content there

i would like to ask how to use the \hyperref because i've googled loads of loads of answers but i can't find. i've created my own TOC. then i included \hyperref package. Then the contents are automatically hyperlinks which is good. but then it went to the wrong pages. i've googled a lot but i couldn't find answer. The contents that i clicked goes to the wrong page.
\documentclass[10pt,a4paper]{report}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{enumerate}
\usepackage{hyperref}
% %\usepackage[colorlinks]{hyperref}
% %\renewcommand*{\contentsname}{\hyperlink{contents}{Contents}}
% %\renewcommand*{\contentsname}{\hyperref[contents]{\arabic{page}}}
\begin{document}
\input{coverPage}
\maketitle
\tableofcontents
\addcontentsline{toc}{chapter}{Table of Contents}
\pagebreak
\addcontentsline{toc}{chapter}{List of Figures}
\listoffigures
\pagebreak
% % (1) ==============================================
\setcounter{chapter}{1}
\addcontentsline{toc}{chapter}{1. Introduction}
\input{introduction}
\pagebreak
\addcontentsline{toc}{chapter}{2. References}
\input{references}
\pagebreak
\addcontentsline{toc}{chapter}{3. Glossary}
\input{glossary}
\pagebreak
\end{document}
The issue here is that \chapter sets itself on a new page, so issuing \addcontentsline{toc}{chapter}{<chapter title>} before \chapter may point to an incorrect page.
Also, your \tableofcontents might be more than a single page. So, issuing \addcontentsline{toc}{chapter}{Table of Contents} after \tableofcontents - a \chapter* - might again point to an incorrect page.
The best solution is to use the following setup:
\cleardoublepage
\addcontentsline{toc}{chapter}{<chapter title>}
% <your \chapter or \chapter*>
This will ensure that \addcontentsline is issued on the same page as \chapter or \chapter*.

How can I get page numbers to link to the table of contents in latex?

I've seen a pdf LaTeX document where the page numbers at the bottom of the page are hyperref links, and clicking them causes you to jump to the contents table. I don't have the tex file and couldn't work out how it's done from the hyperref package. Can anyone help?
You could set an anchor at the toc and redefine \thepage to link to it. Here's an example:
\documentclass{report}
\usepackage[colorlinks]{hyperref}
\renewcommand*{\contentsname}{\hyperlink{contents}{Contents}}
\renewcommand*{\thepage}{\hyperref[contents]{\arabic{page}}}
\begin{document}
\tableofcontents
\chapter{One}
Text
\end{document}
If you use babel and wish to redefine \contentsname, use the \addto command of babel or redefine \contentsname after \begin{document}.
Have you tried defining the page numbering using this?
\pagestyle{myheadings}
\markright{ ... }
where \markright specifies the page number with a link to the content page.
Here is how I did it
% !TEX encoding = UTF-8 Unicode
% !TEX TS-program = xelatex
\documentclass[UTF8, english]{article}
\usepackage{lipsum} %% produce dummy texts
\usepackage{hyperref}
\usepackage[pagestyles]{titlesec}
\newpagestyle{article}{
\setfoot
%% even pages
[]
[\footnotesize \hyperlink{toc}{\thepage}]
[]
%% odd pages
{}
{\footnotesize \hyperlink{toc}{\thepage}}
{}
}
\begin{document}
\title{example}
\date{}
\author{author}
\maketitle
\pagenumbering{roman}
\setcounter{tocdepth}{2}
\addtocontents{toc}{\protect\hypertarget{toc}{}}
\tableofcontents
\newpage
\pagenumbering{arabic}
\pagestyle{article}
\section{A}
\lipsum[1]
\subsection{a}
\lipsum[2]
\subsection{b}
\lipsum[3]
\subsection{c}
\lipsum[4]
\section{B}
\lipsum[5]
\subsection{d}
\lipsum[6]
\subsection{e}
\lipsum[7]
\section{C}
\lipsum[8]
\subsection{f}
\lipsum[9]
\subsection{g}
\lipsum[10]
\subsection{h}
\lipsum[11]
\end{document}
you can of course customize the link text back to table of contents however you like in the preamble, please read documentation of titlesec for more details.

No page number for divider pages in LaTeX

I have a report in LaTeX, and i have used the following commands to create my Appendix, however, my lecturer states that any divider pages should be unnumbered.
\documentclass{report}
\usepackage{appendix}
\begin{document}
\include{chap1}
\include{appendix}
\end{document}
Then in appendix.tex
\appendix
\pagestyle{empty}
\appendixpage
\noappendicestocpagenum
\addappheadtotoc
This creates the Appendices divider page, but still puts a page number on it in the footer. There is no page number in the TOC, as expected.
How can I remove it from the footer?
I looked at the appendix.sty source, and I see the problem: line 74, in the definition of \#chap#pppage, issues a \thispagestyle{plain} command, thus overriding your \pagestyle{empty} for this page. The inelegant but direct way to fix this is to redefine the command without this line - issue the following code after importing the package.
Revised, tested version
\documentclass{report}
\usepackage{appendix}
%==== The action ================
\makeatletter
\def\#chap#pppage{%
\clear#ppage
\if#twocolumn\onecolumn\#tempswatrue\else\#tempswafalse\fi
\null\vfil
\markboth{}{}%
{ \centering \interlinepenalty \#M
\normalfont \Huge \bfseries \appendixpagename\par}%
\if#dotoc#pp\addappheadtotoc\fi
\vfil\newpage
\if#twoside
\if#openright \null \thispagestyle{empty}\newpage\fi
\fi
\if#tempswa \twocolumn\fi
}
\makeatother
%==== Back to the document ========
\begin{document}
\tableofcontents
\chapter{Blah}
Rhubarb, rhubarb, rhubarb.
\appendix
\pagestyle{empty}
\appendixpage
\noappendicestocpagenum
\addappheadtotoc
\chapter{Boff}
Cabbages, cabbages, cabbages.
\end{document}
The TeX FAQ might come in handy here:
I asked for “empty”, but the page is numbered
If you use \pagestyle{empty} and you
find some pages are numbered anyway,
you are probably encountering one of
the style decisions built into the
standard LaTeX classes: that certain
special pages should always appear
with \pagestyle{plain}, with a page
number at the centre of the page foot.
The special pages in question are
those (in article class) containing a
\maketitle, or (in book and report
classes) \chapter or \part commands.
The simple solution is to reissue the
page style after the command, with
effect for a single page, as, for
example (in article):
\maketitle
\thispagestyle{empty}
So give adding \thispagestyle{empty} after your \appendix a try.
try changing \pagestyle{empty} to \thispagestyle{empty} and put it after \addappheadtotoc.
Try instead:
\pagenumbering{gobble}
\begin{appendices} \newpage
\clearpage
\pagenumbering{arabic}
\addtocounter{page}{100}
\tableofcontents
\newpage
*Special thanks to https://tex.stackexchange.com/questions/6639/removing-page-numbers-but-not-headers!

Resources