Graphics, Figures & TablesFigure problem with reference

Information and discussion about graphics, figures & tables in LaTeX documents.
wishotz
Posts: 18
Joined: Mon Dec 26, 2016 2:39 pm

Figure problem with reference

Post by wishotz »

Hello guys,
thanks in advance first of all helping me with this topic. Here is my current problem now. I need for my bachelor paper some help. Currently I have big problems dealing with reference of figures. Im splitting all my chapters in own .tex files and the current problem is this. This is an example in dummy version.
I have a text~\ref{fig:test} and so on blablabal

Code: Select all

\begin{figure}[H]
	\centering
	\includegraphics{xxx/yyy/test}
	\caption{xxx}
	\label{fig:test}
\end{figure}
What im doing here is to call a reference on the upcomming figure, which is defined with float, because I want to have the figure exactly at that point. Problem is now, that if i add parameters to scale the graphic, my reference automatically doesnt work and in my PDF file ?? appears. Sorry for my bad English, I hope you can understand.

Kind Regards,
wishotz
Last edited by wishotz on Mon Dec 26, 2016 3:37 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
TikZ book
BlackForestrian

Figure problem with reference

Post by BlackForestrian »

Welcome to LC

Posting fragments of code is not really helpful. In any case, your code should work if you recompile twice, the label command is placed correctly after the caption macro.

In addition, your fragment does not show any of the mentioned 'scaling' code for the graphics.
wishotz
Posts: 18
Joined: Mon Dec 26, 2016 2:39 pm

Figure problem with reference

Post by wishotz »

BlackForestrian wrote:Welcome to LC

Posting fragments of code is not really helpful. In any case, your code should work if you recompile twice, the label command is placed correctly after the caption macro.

In addition, your fragment does not show any of the mentioned 'scaling' code for the graphics.
I recompiled many times before I make a change since I understand the cross reference. I think im definetly fine with the Syntax. Usually I add something like [scale=0.5] as parameter of incluegraphics, but i left it this time because I wouldn't get the reference successfully. If I add this, it commits a warning which says "Reference 'fig:test' on page X is undefined
BlackForestrian

Figure problem with reference

Post by BlackForestrian »

This 'dummy' document works for me out of the box, using some arbitrary image file...

The references work as well, in addition I suggest to use the cleveref package (not done in my example, however, for sake of simplicity)

Code: Select all

\documentclass{article}
\usepackage{graphicx}
\usepackage{float}

\begin{document}

\begin{figure}[H]
  \centering
  \includegraphics{ente.jpg}
  \caption{xxx}
  \label{fig:test}
\end{figure}

In figure \ref{fig:test} we see \dots, whereas in figure \ref{fig:other} we see another cute, but smaller duck


\begin{figure}[H]
  \centering
  \includegraphics[scale=0.5]{ente.jpg}
  \caption{xxx}
  \label{fig:other}
\end{figure}

\end{document}
Attachments
ente.jpg
ente.jpg (26.94 KiB) Viewed 26977 times
wishotz
Posts: 18
Joined: Mon Dec 26, 2016 2:39 pm

Figure problem with reference

Post by wishotz »

BlackForestrian wrote:This 'dummy' document works for me out of the box, using some arbitrary image file...

The references work as well, in addition I suggest to use the cleveref package.

Code: Select all

\documentclass{article}
\usepackage{graphicx}
\usepackage{float}

\begin{document}

\begin{figure}[H]
  \centering
  \includegraphics{ente.jpg}
  \caption{xxx}
  \label{fig:test}
\end{figure}

In figure \ref{fig:test} we see \dots, whereas in figure \ref{fig:other} we see another cute, but smaller duck


\begin{figure}[H]
  \centering
  \includegraphics[scale=0.5]{ente.jpg}
  \caption{xxx}
  \label{fig:other}
\end{figure}

\end{document}
I tried your code aswell in a seperate file and it worked somehow. But currently its not working for me in my part of text.

These are the things which included:

Code: Select all

\documentclass[12pt,a4paper,titlepage]{article}
% Header importieren
\usepackage{fancyhdr} %Paket laden
\usepackage{caption}
\RequirePackage{amssymb}
\RequirePackage{amsthm}
\RequirePackage[T1]{fontenc} 							%
\RequirePackage[utf8]{inputenc} 						% Umlaute
\RequirePackage[ngerman]{babel}							% Rechtschreibprüfung 
\RequirePackage{graphicx}								% Package um Bilder zu laden 
\RequirePackage{setspace}								%
\RequirePackage{fancyhdr}								% Paket für Kopfzeilen 
\RequirePackage{titlesec}								%
\RequirePackage{tocloft}								%
\RequirePackage[a4paper]{geometry}						% Seitenränder
\usepackage[linesnumbered,ruled]{algorithm2e}
\RequirePackage{array}
\RequirePackage[bookmarksopen=true]{hyperref}
\usepackage{float}
\usepackage{listings}
\usepackage{paralist}
% Tikz
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{backgrounds}
\tikzstyle{decision} = [diamond, draw, fill=blue!20, 
text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=blue!20, 
text width=8em, text centered, rounded corners, minimum height=3em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=red!20, node distance=3cm,
minimum height=2em]

% defining for code fragments
\usepackage{color}
\definecolor{lightgray}{rgb}{.9,.9,.9}
\definecolor{darkgray}{rgb}{.4,.4,.4}
\definecolor{purple}{rgb}{0.65, 0.12, 0.82}

\lstdefinelanguage{JavaScript}{
	keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
	keywordstyle=\color{blue}\bfseries,
	ndkeywords={class, export, boolean, throw, implements, import, this},
	ndkeywordstyle=\color{darkgray}\bfseries,
	identifierstyle=\color{black},
	sensitive=false,
	comment=[l]{//},
	morecomment=[s]{/*}{*/},
	commentstyle=\color{purple}\ttfamily,
	stringstyle=\color{red}\ttfamily,
	morestring=[b]',
	morestring=[b]"
}

\lstset{
	language=JavaScript,
	backgroundcolor=\color{lightgray},
	extendedchars=true,
	basicstyle=\footnotesize\ttfamily,
	showstringspaces=false,
	showspaces=false,
	numbers=left,
	numberstyle=\footnotesize,
	numbersep=9pt,
	tabsize=2,
	breaklines=true,
	showtabs=false,
	captionpos=b
}

% Lösungsboxen
\usepackage{mdframed}
\newif{\ifloesung}
% Lösung aktivieren?
\loesungtrue

\newcommand{\loesung}[1]{\ifloesung \begin{mdframed}[linewidth=0.75pt]#1\end{mdframed} \fi}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\newcommand{\tabitem}{~~\llap{\textbullet}~~}
% Bib Verzeichnis
%\usepackage{multibib}
%\usepackage{hyperref}
%\newcites{Refs,Urls}{Literaturverzeichnis,Verzeichnis der Webadressen}

\linespread{1.5}
Important to mention is, that the documentclass is in my main file. All other stuff are located in packages.tex. The figure which I want to create is a chapter in a seperated file.
BlackForestrian

Figure problem with reference

Post by BlackForestrian »

Important to mention is, that the documentclass is in my main file. All other stuff are located in packages.tex. The figure which I want to create is a chapter in a seperated file.
Well, the documentclass should be of course in the main file, however, your example does not show how you 'generate' the figure at all (I suppose that you are actually trying to place the figure with \includegraphics, not generating it.

The usage of \RequirePackage is possible within documents, but not recommended. Use \usepackage

And consider whether you really need both titlesec and tocloft -- usually they do not like each other!
wishotz
Posts: 18
Joined: Mon Dec 26, 2016 2:39 pm

Figure problem with reference

Post by wishotz »

BlackForestrian wrote:
Important to mention is, that the documentclass is in my main file. All other stuff are located in packages.tex. The figure which I want to create is a chapter in a seperated file.
Well, the documentclass should be of course in the main file, however, your example does not show how you 'generate' the figure at all (I suppose that you are actually trying to place the figure with \includegraphics, not generating it.

The usage of \RequirePackage is possible within documents, but not recommended. Use \usepackage

And consider whether you really need both titlesec and tocloft -- usually they do not like each other!
Well what I actually do is, in my mainfile something like that:

Code: Select all

\subsubsection{test}
\let\clearpage\relax
\include{pages/test/test123}
and in the test123.tex file sth like that:

Code: Select all

pic here ~\ref{fig:test} 
\begin{figure}[H]
	\centering
	\includegraphics{pages/test/testqwkew}
	\caption{test}
	\label{fig:test}
\end{figure}
The graphic which should be placed is a pdf file, so it has a better resolution
BlackForestrian

Figure problem with reference

Post by BlackForestrian »

The reason for your problem is

\let\clearpage\relax

This screws up the whole referencing system.

If you don't want a new page for a separate file, use \input{foo} instead of \include{foo}
wishotz
Posts: 18
Joined: Mon Dec 26, 2016 2:39 pm

Figure problem with reference

Post by wishotz »

BlackForestrian wrote:The reason for your problem is

\let\clearpage\relax

This screws up the whole referencing system.

If you don't want a new page for a separate file, use \input{foo} instead of \include{foo}
Could you explain me this? I dont really understand this. It would be nice since I want to understand the problem aswell.
I want for each chapter a seperate file, thats why i did include.
BlackForestrian

Figure problem with reference

Post by BlackForestrian »

You're using article -- there are no chapters in an article.

You could get the same with \input without screwing up the system by letting a global \clearpage go into nirvana ... ;-)
Post Reply