1

I'm using template with .sty file for writing my phd thesis in LaTeX but I have a problem with headers. In chapters intro and bibliography headers does not say intro and bibliography but the names of chapters that comes before each of them respectively. I tried to fix associated code in .sty file with header commands but it doesn't do anything, all it says is:

\newenvironment{intro}
     {\chapter*{Uvod}%
      \@mkboth{UVOD}{UVOD}%
      \thispagestyle{empty}%
      }{\addcontentsline{toc}{chapter}{Uvod}}

(Uvod is croatian term for intro)

1 Answers1

1

You do not have to define an environment for that. If you use a starred \chapter, you need simply two (or three) additional lines. Here you will write:

\chapter*{Uvod}
\phantomsection
\addcontentsline{toc}{chapter}{Uvod}
\markboth{UVOD}{UVOD}

where the \phantomsection is needed only if you load hyperref and want to add a bookmark pointing to the introduction.

If you want to automatize, you can create a dedicated command \starredchapter by using the patching macros of \usepackage{etoolbox}:

\let\starredchapter=\chapter
\makeatletter
\patchcmd{\starredchapter}{\secdef\@chapter}{}{}{}
\apptocmd{\@schapter}{%
    \phantomsection%
    \markboth{\protect\MakeUppercase{#1}}{protect\MakeUppercase{#1}}%
    \addcontentsline{toc}{chapter}{#1}%
}{}{}
\makeatother

EDIT: The code shown above for defining \starredchapter does work, along as it is executed (1) before to load hyperref (2) after or without \tableofcontents (which also relies on \chapter*). Hence it is useless, at least !

I guess that there are people on this forum who know a simple way to fix this.

A workaround is nevertheless possible with slightly more patching:

\makeatletter
\let\schapter=\chapter
\let\starredchapter=\chapter
\let\@schapterori=\@schapter
\patchcmd{\schapter}{\secdef\@chapter\@schapter}{\@schapterori}{}{}
\patchcmd{\starredchapter}{\secdef\@chapter\@schapter}{\@schapter}{}{}
\patchcmd{\tableofcontents}{\chapter*}{\schapter}{}{}
\apptocmd{\@schapter}{\addcontentsline{toc}{chapter}{#1}\@mkboth{\MakeUppercase{#1}}{\MakeUppercase{#1}}}{}{}
\makeatother

This save the normal behavior of both \tableofcontents and thebibliography, thought enabling them to appear in the toc and bookmarks. Finally a MWE demonstrating that everything works (in standard book class):

\documentclass[a4paper,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[croatian]{babel}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[margin=25mm]{geometry}
\usepackage{etoolbox}
\usepackage{lipsum}
\usepackage{multido}

\makeatletter
\let\schapter=\chapter
\let\starredchapter=\chapter
\let\@schapterori=\@schapter
\patchcmd{\schapter}{\secdef\@chapter\@schapter}{\@schapterori}{}{}
\patchcmd{\starredchapter}{\secdef\@chapter\@schapter}{\@schapter}{}{}
\patchcmd{\tableofcontents}{\chapter*}{\schapter}{}{}
\apptocmd{\@schapter}{\phantomsection\addcontentsline{toc}{chapter}{#1}\@mkboth{\MakeUppercase{#1}}{\MakeUppercase{#1}}}{}{}
\makeatother

\usepackage[colorlinks,bookmarksnumbered,bookmarksopen]{hyperref}

\begin{document}
\tableofcontents

\starredchapter{Uvod}
\lipsum[1-9]
\chapter{Prvo poglavlje}
\section{Prvi stavak}
\lipsum[10-12]
\section{Drugi stavak}
\lipsum[13]

\chapter{Drugo poglavlje}
\lipsum[14]
\multido{\i=3+1,\ii=5+1}{10}{\chapter{\i-th chapter} \section{novi stavak} \lipsum[\i-\ii]\section{drugog stavka}}

\begin{thebibliography}{1}
\multido{\i=10+1}{27}{
\bibitem{Einstein1905photo\i}
Albert Einstein.
\newblock The photoelectric effect.
\newblock {\em Ann. Phys}, 17(132):4, 19\i.
}
\end{thebibliography}
\end{document}

where the \lipsum and the \multido are only used to pad the document.

Sveinung
  • 20,355
Jhor
  • 4,179
  • Thank you. First solution did the job. I changed:

    \newenvironment{intro} {\chapter{Uvod}% @mkboth{UVOD}{UVOD}% \thispagestyle{empty}% }{\addcontentsline{toc}{chapter}{Uvod}} to : \newenvironment{intro} {\chapter{Uvod} \phantomsection \addcontentsline{toc}{chapter}{Uvod} \markboth{UVOD}{UVOD}}

    and it works perfectly now.

    – user193488 Jul 29 '19 at 13:59
  • If you are happy with this, its perfect. But let me nevertheless insist on the point that it is a bad practice to use an environment where only sectioning headers are changed, and furthermore to create such structure that you will use only once. By the way the closing command of the environment seems to be missing in the code you posted in the comment. – Jhor Jul 29 '19 at 14:11