Latex Table of Contents Links to Wrong Section - latex

I have a section followed by a table of contents like so:
\section{Section1}
ABC.
\addcontentsline{toc}{section}{Table of Contents}
\tableofcontents
\newpage
\addcontentsline{toc}{section}{List of Figures}
\listoffigures
\newpage
\section{Section2}
DEF.
\section{Section3}
GHI.
My issue is that the "Table of Contents" and "List of Figures" entries in the table of contents link (in the generated pdf) to the wrong place in the file. They both link to the first section section on page 1. The entry in the table is correct (TOC says page 2 and LOF says page 3), but the link goes to the wrong place.

You'll need to use the \phantomsection command:
\section{Section1}
ABC.
\phantomsection
\addcontentsline{toc}{section}{Table of Contents}
\tableofcontents
\newpage
\phantomsection
\addcontentsline{toc}{section}{List of Figures}
\listoffigures
\newpage
\section{Section2}
DEF.
\section{Section3}
GHI.
See the hyperref manual.

If you're doing this for the bibliography, list of tables or list of figures,
\usepackage[nottoc]{tocbibind}
should fix it, without the wrong-page problems. Otherwise, I havent come across a better solution than \phantomsection with \addcontentsline.

This behavior is due to the fact that \tableofcontents inserts a page break before writing the contents. Hence, your PDF bookmark will point to the page before. Depending on your document class, you can manually insert a number of \newpage commands to keep \tableofcontents from adding another. One or two should be sufficient.
I know, it is a hacky solution, and there might exist a package to solve the problem, but this is how I work around the problem.

Related

Latex section without a number in the table of contents

I would like to create a table of contents for a bachelorthesis but dont wanna nummerate all points of it.
The abstract for example should be in the toc but not with a number.
I write my thesis in the documentclass article where i dont have the chance to use \chapter.
Is there an easy way to fix the problem?
Thank you!
You can use \section*{} which create a section without the numeration.
However, it will not be present in the table of content.
You can manually add it with \addcontentsline{toc}{section}{\protect\numberline{}Your section name}.
For example for an abstract, an introduction, two numbered sections, and a conclusion, you can do:
\documentclass{article}
\begin{document}
\tableofcontents
\section*{Abstract}
\addcontentsline{toc}{section}{\protect\numberline{}Abstract}
\section*{Introduction}
\addcontentsline{toc}{section}{\protect\numberline{}Introduction}
\section{Section 1}
\section{Section 2}
\section*{Conclusion}
\addcontentsline{toc}{section}{\protect\numberline{}Conclusion}
\end{document}

Use \newcommand without the text styles added to it

This is a weird question and I don't know if I was able to formulate it that well.
So, I declare a newcommand somewhere like this:
\newcommand{\examplecommand}{\textbf{\textit{exampletext}}}
It'll be used quite many times this way.
I'm trying to also use it in one place in the document, wanting to format it in a different way (with \textsc{}, and none of the bolding and italicization)
I tried doing stuff like
\textsc{\examplecommand}
But that doesn't work, it still somehow prioritizes the formatting in the command declaration.
How can I use the command without changing the declaration, but with different formatting?
EDIT (MWE):
document.tex
--------------------
\documentclass{book}
\usepackage{fancyhdr}
\usepackage{blindtext}
\newcommand{\booktitle}{} % create it empty at first, so that the files can change it
\newcommand{\textbfit}[1]{\textbf{\textit{#1}}} % combine bold and italic in one
\fancypagestyle{plain}{
\fancyhf{}
\fancyhead[RO,RE]{\textsc{\booktitle}}
\renewcommand{\headrulewidth}{2pt}
}
\begin{document}
\pagestyle{plain}
\chapter{example1}
\input{doc1}
\chapter{example2}
\input{doc2}
\chapter{example3}
\input{doc3}
\end{document}
doc1.tex
--------------------
\renewcommand{\booktitle}{\textbfit{``Title 1''}}
\booktitle\ is a very nice book, it's really great, buy it etcetcetc
\blindtext
doc2.tex
--------------------
\renewcommand{\booktitle}{\textbfit{``Title 2''}}
\booktitle\ is a very nice book, it's really great, buy it etcetcetc
\blindtext
doc3.tex
--------------------
\renewcommand{\booktitle}{\textbfit{``Title 3''}}
\booktitle\ is a very nice book, it's really great, buy it etcetcetc
\blindtext
The reason I want to be able to do this:
I have 12 documents that I wrote in a single-document form, and now I wish to make them work both in the single-document form (all of them having a separate .tex file that compiles them as a single document), and a form that puts them all into a large file.
I was using that \booktitle command across all of them, since they are all similar in general structure. And now when I wanted to also convert them into book form, I discovered I can use it to change the top right corner text inside the header (see that the definition of the plain fancypagestyle uses that command inside its fancyhead), but, in there, I want to use it with \textsc
The reason I don't want to change my command is because it means changing it across all of the documents inside, and I was just thinking I could do \textsc{\booktitle} and be done with it
You could temporarily switch off your \textbfit command:
\documentclass{book}
\usepackage{fancyhdr}
\usepackage{blindtext}
\newcommand{\booktitle}{} % create it empty at first, so that the files can change it
\newcommand{\textbfit}[1]{\textbf{\textit{#1}}} % combine bold and italic in one
\fancypagestyle{plain}{
\fancyhf{}
\fancyhead[RO,RE]{
\begingroup
\let\textbfit\relax
\textsc{\booktitle}
\endgroup
}
\renewcommand{\headrulewidth}{2pt}
}
\begin{document}
\pagestyle{plain}
\chapter{example1}
\renewcommand{\booktitle}{\textbfit{``Title 1''}}
\booktitle\ is a very nice book, it's really great, buy it etcetcetc
\blindtext
\end{document}

LATEX: delete the number of page from the backmatter

im tring to delete the page number from my \backmatter sequence. I've succeded to do this, writing this code:
\begin{document}
.
.
.
\backmatter
\begingroup
\makeatletter
\let\ps#plain\ps#empty
\addcontentsline{toc}{chapter}{Allegati}
\input{Allegati}
\input{Bibliografia}
\listoffigures
\listoftables
\listof{grafico}{Elenco dei grafici}
\endgroup
\end{document}
But, I don't know why, the last page of my document still have his number. In the code above the last page is a graphic list, but if I change the order of my sequence, for example putting the bibliography in the last position, the this element will present the page number. I'm writing this document using the book class.
Someone can help me?
thanks
As you did not provide a MWE (Minimal Working Example) I can just guess what your document's preamble contains.
Have a look at this code please:
\documentclass{book}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancypagestyle{plain}{
\lhead{}
\chead{}
\rhead{}
\lfoot{}
\cfoot{}
\rfoot{}
}
\begin{document}
text
\newpage
text
\newpage
\backmatter
\begingroup
\pagestyle{plain}
\addcontentsline{toc}{chapter}{Allegati}
\listoffigures
\listoftables
%\listof{grafico}{Elenco dei grafici}
\endgroup
\end{document}
EDIT1:
This way you redefine the plain-style that is used by every chapter-page in your document.
If you do not want to change it document-wide, your approach is correct; just add a \clearpage before the last \endgroup. It won't create a new page, but delete the pagenumber. Btw, if you don't really need the \begingroup \endgroup leave it out, this will also delete the last page number.
END-EDIT1
A few things to note:
If you use plain book-class, you can use fancyhdr for setting a
page style like in the above example. Later just load the wanted
pagestyle and from there on it will be used.
If you want to write a document according to the modern
standard of LaTeX, maybe think about using a KOMA-class, which
provides great functions for changing the page style (and often they
also work better ;))
For me this MWE does work, having no pagenumber on the last page; if it does not for you, please give us more information about your document.

Latex : Understand memoir pagestyle

I have a sample of doc in latex and would like you to explain why this is not working as expected.
This is the code:
\documentclass{memoir}
\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{background}
\begin{document}
\SetBgContents{plain}
\pagestyle{plain}
\makeoddhead{plain}{}{}{header test}
\blinddocument
\clearpage
\blinddocument
\clearpage
\blinddocument
\clearpage
\end{document}
After compilation:
Indentation of the page changes from page to page
"header test" some time appears, sometime not
There are blank pages when I explicitly put "clearpage"
Can you please explain how to solve and make something stable ?
Thanks
Unless specified otherwise, the default option passed to memoir is to set the document in twoside mode. See p 5 of the memoir documentation under REMARKS:
Calling the class with no options is equivalent to:
\documentclass[letterpaper,10pt,twoside,onecolumn,openright,final]{memoir}
Under twoside, the odd and even pages are offset from the inner margin, or the gutter. This results in the differing "indentations" of the page.
\makeoddhead only sets the header for odd pages. Odd pages only occur every other page, leaving even page headers empty (the default for the plain page style).
\clearpage flushes all pending floats and starts a new page. There should be no blank page issued. However, with a call to \chapter, memoir actually issues \clearforchapter, which is similar to \cleartorecto or \cleardoublepage. This necessarily clears pages until an odd page is reached, ensuring that a chapter starts on a right.

minitoc: updating tocdepth counter

I am writing my thesis using LaTeX and I would like to include at the beginning of each chapter a mini table of contents including only sections. I am using the minitoc package which I declare in my preamble file. In my main file, I found myself obliged to include the following for the the minitoc to update itself:
% *********************** Adding TOC and List of Figures ***********************
\dominitoc
\tableofcontents
\listoffigures \mtcaddchapter
\listoftables \mtcaddchapter
As I am setting the section counter to 2:
\setcounter{secnumdepth}{2}
\setcounter{tocdepth}{2}
The mini tables of contents I am getting includes the subsections (which seems the default minitoc section depth). I tried changing the section toc depth by including:
\newcounter{secttocdepth}
\setcounter{secttocdepth}{1}
everywhere with no success. Your help is very welcome!
Have you tried using
\setcounter{minitocdepth}{1}
like described by the minitoc manual

Resources