4

Reference to a section is not problematic but referencing to a chapter produces some strange spacing after the number of chapter. See attached picture:

enter image description here

This is my code

\documentclass[12pt,a4paper,twoside,fleqn,openright]{book}

% use quite a lot of packages
\usepackage{amsfonts,amssymb,amsmath,bm}
\usepackage{enumerate}
\usepackage[section]{placeins}
\usepackage{float}
\usepackage[slovene]{babel}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{ifthen}
\usepackage[customcolors]{hf-tikz}
\usepackage{fancyhdr}
\usepackage{longtable}
\usepackage{hyperref}
\usepackage{ifoddpage}
\usepackage{tikz}
\usepackage{tocloft}
\usepackage{titlesec}
\usepackage{pdfpages}
\usepackage{nameref}
\usepackage{multirow}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{enumitem}
\usepackage[numbib]{tocbibind}
\usepackage{url}
\usepackage{cite}
\usepackage{upgreek}
\usepackage[inner=30mm,
            outer=25mm,
            top=30mm,
            bottom=25mm]{geometry}

% paragraph settings
\setlength\parindent{0pt}
\setlength{\parskip}{1.5ex plus 0.5ex minus 0.5ex}

% set equation environment indentation
\setlength{\mathindent}{0.5cm}%

% set itemize environment whitespacing and left margin
\setlist[itemize]{noitemsep,nolistsep, leftmargin=*}

% set table and figure captions
\captionsetup[table]{skip=10pt,singlelinecheck=false}
\captionsetup[figure]{justification=centering}

% set tablename to Preglednica
\AtBeginDocument{%
  \renewcommand\tablename{Preglednica}
}

% command for multiline cell in table
\newcommand{\minitab}[2][l]{\begin{tabular}{#1}#2\end{tabular}}

\newcommand{\vect}[1]{\boldsymbol{\mathbf{#1}}}


% set section and tableofcontents depth
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}
\def\labelitemi{--}

%  accordingly format a chapter definition
\titleformat{\chapter}[display]
  {\bfseries}{}{0pt}{\Huge\thechapter}

% set fancy_nohead fancy header
\fancypagestyle{fancy_nohead}{
\fancyfoot[LE,RO]{\thepage}
\renewcommand{\headrulewidth}{0pt}
\chead{}
\cfoot{}
}

% assign no_header
\assignpagestyle{\chapter}{fancy_nohead}

% section and chapter formatting
\renewcommand{\thechapter}{\arabic{chapter}.\quad}
\renewcommand{\thesection}{\arabic{chapter}.\arabic{section}.}
\renewcommand{\thesubsection}{\thesection\arabic{subsection}.}
\renewcommand{\thesubsubsection}{\thesubsection\arabic{subsubsection}.}
\renewcommand{\thetable}{\arabic{chapter}.\arabic{table}}
\renewcommand{\thefigure}{\arabic{chapter}.\arabic{figure}}
\renewcommand{\theequation}{\arabic{chapter}.\arabic{equation}}
\renewcommand\labelenumi{(\theenumi)}
\DeclareMathOperator{\E}{\mathbb{E}}
\def\checkmark{\tikz\fill[scale=0.4](0,.35) -- (.25,0) -- (1,.7) -- (.25,.15) -- cycle;} 
\begin{document}
\chapter{Test}\label{cha:test}
Something written here.
\chapter{New test}\label{cha:new_test}
Reference to Chapter \ref{cha:test} \nameref{cha:test}.
\end{document}

And one line has to be problematic because simply writing

\documentclass[12pt,a4paper,twoside,fleqn,openright]{book}
\usepackage[slovene]{babel}
\usepackage[utf8]{inputenc}

\usepackage{hyperref}

\begin{document}
\chapter{Test}\label{cha:test}
Something written here.
\chapter{New test}\label{cha:new_test}
Reference to Chapter \ref{cha:test} \nameref{cha:test}.
\end{document}

is not problematic anymore.

Does anybody have an idea which of the packages is problematic or maybe knows a workaround?

Werner
  • 603,163
skrat
  • 143
  • 3
    You renew the \thechapter command to contain \quad. That's all. Remove that extra horizontal space and you're done. – TeXnician Jun 30 '17 at 12:18
  • Welcome to TeX.SE. The culprit is the instruction \renewcommand{\thechapter}{\arabic{chapter}.\quad} -- what is the purpose of this instruction, especially the \quad component? – Mico Jun 30 '17 at 12:20
  • @Mico I prefer the visual outcome of the Chapter. This should (and it does) produce a \quad spacing between the number of the Chapter and the name of the chapter. Example: \chapter{Test} is visible as 1.____Test. But as @TeXnician found out this also corrupts my referencing. Is there a way to keep both - my spacing in the chapters and not destroy the reference to it? – skrat Jun 30 '17 at 12:24
  • As an aside, see Which packages should be loaded after hyperref instead of before? Typically, hyperref should be loaded last, and most definitely after sectioning and other reference packages (like titlesec, tocloft and caption). In some cases it's not necessary, but it's definitely preferred/advised. – Werner Jun 30 '17 at 12:37

2 Answers2

6

Changing the way the chapter counter is represented is the issue here. That is, the line

\renewcommand{\thechapter}{\arabic{chapter}.\quad}

causes the extra space (a \quad) to be added to your reference. Most likely you've redefined \thechapter because you want some additional space between the chapter number and its title in the actual chapter heading. This can be achieved through your use of titlesec's \titleformat:

\titleformat{\chapter}[display]
  {\bfseries}{}{0pt}{\Huge\thechapter.\quad}

Also, since your other sectioning number redefinitions end in ., references to these will also include a period . in it, which is not ideal. Instead, redefine the hierarchically and without the trailing period. Rather add the trailing period as part of the sectioning display. One way is to redefine \@seccntformat (see How to add a dot after the section number?):

\makeatletter
\renewcommand{\@seccntformat}[1]{#1.}
\makeatother

By hierarchically I mean using \the... for any parent counter in the subordinate counter representation:

\renewcommand{\thechapter}{\arabic{chapter}}
\renewcommand{\thesection}{\thechapter.\arabic{section}}
\renewcommand{\thesubsection}{\thesection.\arabic{subsection}}
\renewcommand{\thesubsubsection}{\thesubsection.\arabic{subsubsection}}

The above set of commands is somewhat superfluous, as this is the default for the book class anyway.

Werner
  • 603,163
2

Your problem is that you have a \quad (which creates horizontal space) in your definition of \thechapter. Remove it and you're done. If you want to add some space within the chapter's line use titlesec or a similar package (or see here).

And the other lines renewing \theX commands should probably be replaced by a \usepackage{chngcntr} and then many \counterwithin{X}{chapter}.

Maybe you can also reconsider the usage of so many packages too. Probably you do not make use of all of them. Try removing the unnecessary ones. One example is float which only really serves to provide the H float option which should not be used (see here).

TeXnician
  • 33,589