3

In my LaTeX document I have some figures. They are automatically numbered using the chapters they are within, for example. Fig 1.1, Fig 2.3 etc. I am using mwart document class. My problem is that the hyperlinks that are created for some of the figures (using \ref and \listoffigures) are wrong. For example if i click on the hyperlink that should lead to figure 2.1, it leads me to the 1.1 instead. Same for 2.2 -> 1.2 etc. What can be the issue here? Just for the sake of testing, I have created some additional dummy figures in chapter 2, so I have more figures there than in chapter 1. When I click on figure 2.7 it takes me to 2.7, since the highest number figure in chapter 1 is only 1.6... I would really appreciate all help here. Please let me know what additional information is needed to solve this problem and I will provide them.

This is a test document showing the issue:

%% Clear sans, courier

% Klasa:
\documentclass[12pt]{mwart}

% Kodowanie:
\usepackage[utf8]{inputenc}

% Czcionki:
\usepackage[sfdefault]{ClearSans} %% option 'sfdefault' activates Clear Sans as the default text font
\usepackage[T1]{fontenc}
\usepackage{courier}
\usepackage[T1]{polski}

% Geometria i style
%\usepackage{showframe} % debug
\usepackage{geometry}
\geometry{a4paper,twoside=true,top=20mm,bottom=30mm,inner=35mm,outer=20mm}
\pagestyle{plain}
\usepackage{float}
\usepackage{afterpage}

% Grafika
\usepackage{graphicx}

% kody
\usepackage{listings}

% matma
\usepackage{amssymb,amsmath}

% theoremy
\usepackage{amsthm}

% Numerowanie obiektow wedlug rozdzialow
\usepackage{chngcntr}
\counterwithin{figure}{section}
\numberwithin{equation}{section}

% Linki
\usepackage{url}

% Listy
\usepackage[ampersand]{easylist}
\ListProperties(Hide=100, Hang=true, Progressive=3ex, Style*=-- ,
Style2*=$\bullet$ ,Style3*=$\circ$ ,Style4*=\tiny$\blacksquare$ )

% Zmienne srodowiskowe:
\newcommand{\insertImg}[3]{
\begin{figure}[h]
    \centering
    \includegraphics[height=50mm]{#1}
    \caption{#2} \flushleft{Źródło: Na podstawie \cite{#3}}
    \label{#1}
\end{figure}
}
\newcommand{\insertImgSetSize}[4]{
\begin{figure}[h]
    \centering
    \includegraphics[height=#2mm]{#1}
    \caption{#3} \flushleft{Źródło: Na podstawie \cite{#4}}
    \label{#1}
\end{figure}
}

% Indeksowanie w pdfie wyjsciowym
\usepackage{hyperref}

% START:
\begin{document}
\thispagestyle{empty} % usun nr strony ze strony tytulowej

\section{test 1}

\insertImg{cat1.png}
    {caption 1}
    {nothing}

\section{test 2}

\insertImg{cat2.png}
    {caption 2}
    {nothing}

link should lead to 2.1 but will lead to 1.1 \ref{cat2.png}.


\afterpage{\null \thispagestyle{empty} \newpage}
\clearpage

\end{document}

Link to the output pdf as I cant add an attachement here: https://dl.dropboxusercontent.com/u/14154631/test.pdf

3
  • As it stands, this is not answerable yet. Please post a document that shows this issue!
    – user31729
    May 15, 2016 at 15:40
  • I have added the link to the doc, I had to commit. May 15, 2016 at 15:42
  • 1
    Please prepare a minimal working example and post it as part of the question. External links can break, leaving the question/answer unuseful.
    – Johannes_B
    May 15, 2016 at 15:50

2 Answers 2

3

The internal \theH<counter> is already explained by Christian Hupfer's answer. The answer also shows, how to fix it in a general way.

In this case, there is an alternative way. hyperref refines \@addtoreset, which is used by \counterwithin and \numberwithin to support \theH<counter>. However, if these commands are used before hyperref is loaded, the support comes too late. Therefore, an easy fix is to move \counterwithin and \numberwithin after hyperref:

\usepackage{hyperref}
\counterwithin{figure}{section}
\numberwithin{equation}{section}
2

The usual error: section resets the figure counter, in this case the figure and the \theHfigure macro produce a wrong anchor.

Use something like

\renewcommand{\theHfigure}{figure.section.\thesection.\thefigure}

to cure this, i.e. a section number dependent anchor.

In addition, there were some % missing in the \InsertImg{...}. macros.

%% Clear sans, courier

% Klasa:
\documentclass[12pt]{mwart}

% Kodowanie:
\usepackage[utf8]{inputenc}

% Czcionki:
\usepackage[sfdefault]{ClearSans} %% option 'sfdefault' activates Clear Sans as the default text font
\usepackage[T1]{fontenc}
\usepackage{courier}
\usepackage[T1]{polski}

% Geometria i style
%\usepackage{showframe} % debug
\usepackage{geometry}
\geometry{a4paper,twoside=true,top=20mm,bottom=30mm,inner=35mm,outer=20mm}
\pagestyle{plain}
\usepackage{float}
\usepackage{afterpage}

% Grafika
\usepackage{graphicx}

% kody
\usepackage{listings}

% matma
\usepackage{amssymb,amsmath}

% theoremy
\usepackage{amsthm}

% Numerowanie obiektow wedlug rozdzialow
\usepackage{chngcntr}
\counterwithin{figure}{section}
\numberwithin{equation}{section}


% Linki
\usepackage{url}

% Listy
\usepackage[ampersand]{easylist}
\ListProperties(Hide=100, Hang=true, Progressive=3ex, Style*=-- ,
Style2*=$\bullet$ ,Style3*=$\circ$ ,Style4*=\tiny$\blacksquare$ )

% Zmienne srodowiskowe:
\newcommand{\insertImg}[3]{%
\begin{figure}[h]
    \centering
    \includegraphics[height=50mm]{#1}
    \caption{#2} \flushleft{Źródło: Na podstawie \cite{#3}}
    \label{#1}
\end{figure}
}
\newcommand{\insertImgSetSize}[4]{%
\begin{figure}[h]
    \centering
    \includegraphics[height=#2mm]{#1}
    \caption{#3} \flushleft{Źródło: Na podstawie \cite{#4}}
    \label{#1}
\end{figure}
}

% Indeksowanie w pdfie wyjsciowym
\usepackage{hyperref}

\renewcommand{\theHfigure}{figure.section.\thesection.\thefigure}


% START:
\begin{document}
\thispagestyle{empty} % usun nr strony ze strony tytulowej

\section{test 1}

\insertImg{ente1.jpg}
    {caption 1}
    {nothing}

\section{test 2}

\clearpage

\insertImg{ente2.jpg}
{caption 2}
{nothing}

link should lead to 2.1 but will lead to 1.1 \ref{ente2.jpg}.


\afterpage{\null \thispagestyle{empty} \newpage}
\clearpage

\end{document}
8
  • Thank you very much. Could you only please explain why did this fix the issue so I will know how to use it later? May 15, 2016 at 16:37
  • 1
    @ŁukaszPrzeniosło: I've added the statement at the top. It's the resetting of the figure counter, i.e. the anchor figure.1 etc. occurs multiple times, that's why the link lead back to the first figure
    – user31729
    May 15, 2016 at 16:38
  • I could not fix the unreferenced cites, however
    – user31729
    May 15, 2016 at 16:39
  • The cites work fine, i typed nothing in the examplse as there ware no cites added. Now I understand that renewcommand. Thank you very much for your time. May 15, 2016 at 16:41
  • @ŁukaszPrzeniosło: I meant, that I can't fix the cites because I don't have the .bib sources.
    – user31729
    May 15, 2016 at 16:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .