# Problem while referencing chapters

I am currently finishing my thesis. Being new to Latex I wrote 99% of the document in a WYSIWYG-type software then exported it as a Latex project and worked it around a bit into a decent project. I am almost done but I found two problems, one of them is that when referencing chapters the compiled text does not reflect the referenced label.

\chapter{Introducción}

\label{cap:1}

\hypertarget{sec:1.1}{\section{Sistemas de error de foco y medio óptico de almacenamiento}\label{sec:1.1}}

This is a reference to Chapter \ref{cap:1}. And this is a reference to Section \ref{sec:1.1.1}.
Here I mean to reference chapter two, but it compiles like a section to Section one point three like so  \ref{cap:2}.
Which is the last label before label "cap:2".

{\color[rgb]{0.2627451,0.2627451,0.2627451}
\hypertarget{sec:1.1.1}{\subsection{Reseña}}\label{sec:1.1.1}}


And what I get is:

Sections, Subsections and Subsubsections are referenced properly. The First chapter is not referenced at all and other chapters' references are compiled as references to the last valid label. I have looked at other questions and other examples and none of them seem to have this problem.

I wrote every chapter on a separate file and then included them in the main like this:

%Empiezo a contar las páginas en arábigos
\pagenumbering{arabic}
% Agrego los capítulos
\setcounter{chapter}{1}
\include{Capitulo_I/Capitulo_I}
\setcounter{chapter}{2}
\setcounter{section}{0}
\setcounter{equation}{0}
\setcounter{table}{0}
\setcounter{figure}{0}
\include{Capitulo_II/Capitulo_II}


My preamble is a mess, as I took it from a book on Latex and then added stuff I needed, some was added by collaborators, I copy it here just in case.

\documentclass[a4paper, 11pt, twoside, openright, titlepage]{book}
\frontmatter
% Basic packages
\usepackage[spanish]{babel}
\usepackage{float} % For new floating environments
\usepackage{stmaryrd,amssymb,amsmath} % For mathematical symbols and equations
\usepackage{array} % For arrays of equations
\usepackage{epsfig,graphicx,subfigure} % For inserting figures
\usepackage{sidecap}
\sidecaptionvpos{figure}{c}
\usepackage{wrapfig} % For wraping texts around tables and figures
\usepackage{tabularx} % For auto-adjusted column widths in tables
\usepackage{multirow} % For merging cells in tables
\usepackage{multicol} %Para usar varias columnas
\setlength{\columnsep}{0.5cm}
\usepackage{longtable} % For multi-page tables
\usepackage{rotating} % For rotating a page (landscape) or inclined texts
\usepackage{caption} % For adjusting captions of tables and figures
\captionsetup[table]{skip=10pt}
\usepackage{color} % For writing colored texts
\usepackage{setspace} % For adjusting line spacing
%\usepackage{boxedminipage,fancybox} % For boxed texts
%\usepackage{shadow} % For creating shaded box
\usepackage{natbib} % For bibliographic references
\usepackage{varioref} % For referring through \vref{} & \vpageref{}
\usepackage{url} % For citing URL
\usepackage{makeidx} % For generating index
\usepackage[top=2.5cm,bottom=2.5cm,left=2.5cm,right=2.5cm,head,foot]{geometry}

\usepackage[utf8]{inputenc} %para escribir acentos (letras no anglosajonas)
\setcounter{secnumdepth}{4} %para que ponga 1.1.1.1 en subsubsecciones
\setcounter{tocdepth}{4} % para que ponga subsubsecciones en el indice
\usepackage{hyperref} % Para Hyperenlaces
\usepackage{supertabular}
\usepackage{hhline}
\makeatletter
\newcommand\arraybslash{\let\\\@arraycr}
\makeatother
%\usepackage{siunitx}
\usepackage[table]{xcolor}
\usepackage[graphicx]{realboxes}
\usepackage{rotating}
\usepackage{pdflscape}
\usepackage{afterpage}
\usepackage{textcomp}
\usepackage[capitalise]{cleveref}
%
\makeindex % Generate index
% Blank space adjustment
\abovecaptionskip % Skips extra space above a caption
\belowcaptionskip % Skips extra space below a caption
\raggedbottom % Top aligning a page leaving space at the bottom
% User-defined new commands
\definecolor{ugray}{gray}{0.25} %User-defined gray color ‘ugray’
\newcommand{\tgray}{\textcolor{ugray}} % ‘\tgray{}’ for writing in user-defined ‘ugray’
\newcommand{\tred}{\textcolor{red}} % ‘\tred{}’ for writing in red
\newcommand{\vctr}[1]%
{\mbox{\boldmath{$#1$}}} % Prints x as a vector through \vctr{x}
%\newtheorem{thm}{Theorem} % Environment ‘thm’ for writing theorems
%\newtheorem{dfn}{Definition} % Environment ‘dfn’ for writing definitions
%\newtheorem{lem}{Lemma} % Environment ‘lem’ for writing lemmas

\DeclareMathOperator{\sen}{sen}
\DeclareMathOperator{\tg}{tg}
\DeclareMathOperator{\arctg}{arctg}
\DeclareMathOperator{\tgh}{tgh}

\addto\captionsspanish{\renewcommand{\listfigurename}{Índice de Figuras}}
\addto\captionsspanish{\renewcommand{\listtablename}{Índice de Tablas}}
\addto\captionsspanish{\renewcommand{\tablename}{Tabla}}
\definecolor{verdecito}{rgb}{0.7, 0.95, 0.8}

\newcommand\scalemath[2]{\scalebox{#1}{\mbox{\ensuremath{\displaystyle #2}}}}


Any help is appreciated. Best regards!

• As always on this site provide a full but minimal self contained example others can copy and try as is. Not these sniplets. And remove irrelevant packages. Jun 17 '20 at 13:26
• you should not have to do any of this!! \setcounter{chapter}{2} \setcounter{section}{0} \setcounter{equation}{0} \setcounter{table}{0} \setcounter{figure}{0} The resetting of counters should be automatic, also you should not need \hypertarget here \hypertarget{sec:1.1.1}{\subsection{Reseña}}\label{sec:1.1.1}} section headings are already targets. It is best to avoid using numbers in \label and \ref keys (they do work but are unrelated to the numbers that are printed, so it can be confusing) Jun 17 '20 at 13:38
• Oh, Your image seems to show that your Introduction chapter is not numbered, and you set the counter using \setcounter That does not set up a counter for \label (you need \refstepcounter{chapter} But chapters would be numbered by default, where have you stopped that? Jun 17 '20 at 13:45
• \abovecaptionskip % Skips extra space above a caption \belowcaptionskip % Skips extra space below a caption does not do what the comments suggest, this is an assignment \abovecaptionskip = \belowcaptionskip making the caption have the same space above and below Jun 17 '20 at 13:48
• @daleif That's what I meant, sorry if it was not up to site standards, I left irrelevant packages knowingly as I thought some might be producing this behaviour. I edited the post, but as it already got answered I decided to leave it like this. Jun 17 '20 at 14:17

## 1 Answer

It is a really bad idea to start from an undocumented template, better to start from \documentclass{book} and an empty preamble, and only add things that you know you need.

Your main problem here is caused by

\frontmatter


That should never be in the preamble it should (if used at all) be after \begin{document} and followed after a few front matter pages by \mainmatter to restore normal behaviour.

\frontmatter disables counting and labelling of chapters to give unnumbered headings for things like table of contents.

If you remove that then chapter numbering will be restored and you will also be able to replace

            \setcounter{chapter}{1}
\include{Capitulo_I/Capitulo_I}
\setcounter{chapter}{2}
\setcounter{section}{0}
\setcounter{equation}{0}
\setcounter{table}{0}
\setcounter{figure}{0}
\include{Capitulo_II/Capitulo_II}


by

            \include{Capitulo_I/Capitulo_I}
\include{Capitulo_II/Capitulo_II}


Do not load the epsfig package in any document, and only load packages that you use (it is unlikely that you use most of the ones you are loading), do you have both supertabular and longtable for example?

\hypertarget{sec:1.1}{\section{Sistemas de error de foco y medio óptico de almacenamiento}\label{sec:1.1}}


can be simply

\section{Sistemas de error de foco y medio óptico de almacenamiento}\label{sec:1.1}


although it is better to avoid numbers and use \label{Sistemas-de-error} or some such.

The construct

    \abovecaptionskip % Skips extra space above a caption
\belowcaptionskip % Skips extra space below a caption


is actually a short form of

    \abovecaptionskip=\belowcaptionskip


so sets the above skip to the belowskip (which is 0pt by default in book), if the lines were in the other order it would set them both to 10pt.