## LaTeX forum ⇒ Articles, Essays, and Journal Templates ⇒ Problem with subfloats and \vref Topic is solved

IEEE, Elsevier, ACM, ACE, Science, AGU, PNAS, PLOS, RSoC, Springer, Stylish articles and Essays
VitorPossebom
Posts: 4
Joined: Sat May 09, 2015 3:41 am

### Problem with subfloats and \vref

Hello,

I want to create a figure (figure 1) with two subfigures (subfigures 1a and 1b) and to use \vref to make references to the subfigures in my text. When I insert the figure's caption at the end of the environment "figure" and write \vref{levelintuition} or \vref{growthintuition} as I do in the example below, I have no problem at all.

\begin{figure}[h]\centering\subfloat[Linear projection of tuition prices into ENEM results\label{levelintuition}]{\includegraphics[width=.45\columnwidth]{model1.png}} \quad\subfloat[Correlation between tuition prices and ENEM results\label{growthintuition}]{\includegraphics[width=.45\columnwidth]{model2.png}}\caption{Intuitive explanation of Models \ref{level} and \ref{growth}\label{intuition}}\end{figure}

However, I want to insert the figure's caption above the figure as I do when I insert a table. In this case, I typed the code below. As I wished, the figure's caption is now written above it. But, when I use \vref{levelintuition} or \vref{growthintuition}, the program writes subfigures 2a and 2b instead of 1a and 1b. How can I solve this numbering problem?
\begin{figure}[h]\caption{Intuitive explanation of Models \ref{level} and \ref{growth}}\label{intuition}\centering\subfloat[Linear projection of tuition prices into ENEM results\label{levelintuition}]{\includegraphics[width=.45\columnwidth]{model1.png}} \quad\subfloat[Correlation between tuition prices and ENEM results\label{growthintuition}]{\includegraphics[width=.45\columnwidth]{model2.png}}\end{figure}

Thank you very much for your help,

Vítor.

PS: It is the first time I asked a question about LaTeX. Consequently, I am not sure if I provided enough information about my problem. If you need more information, I will gladly provide.

Tags:

Johannes_B
Site Moderator
Posts: 4038
Joined: Thu Nov 01, 2012 4:08 pm
Hi and welcome,

a compilable example is always a good idea, as it saves some time for the helpers.

To the problem at hand, i cannot reproduce this. PLease test the following example.
\documentclass{article}\usepackage{graphicx}\usepackage{subfig}\begin{document}\begin{figure}[h]	\centering	\caption{Intuitive explanation of Models \ref{level} and \ref{growth}\label{intuition}}	\subfloat[Linear projection of tuition prices into ENEM	results\label{level}]{\includegraphics[width=.45\columnwidth]{example-image-a}}	\quad	\subfloat[Correlation between tuition prices and ENEM	results\label{growth}]{\includegraphics[width=.45\columnwidth]{example-image-b}}	\end{figure}	\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.

VitorPossebom
Posts: 4
Joined: Sat May 09, 2015 3:41 am
Thank you very much, Johannes_B.

The funny thing is that, when I run the following code,

\documentclass{article}\usepackage{graphicx}\usepackage{subfig}\begin{document}\begin{figure}[h]\centering\caption{Intuitive explanation of Models 1 and 2}\label{intuition}\subfloat[Linear projection of tuition prices into ENEM results\label{levelintuition}]{\includegraphics[width=.45\columnwidth]{model1.png}} \quad\subfloat[Correlation between tuition prices and ENEM results\label{growthintuition}]{\includegraphics[width=.45\columnwidth]{model2.png}}\end{figure}Subfigures \ref{levelintuition} and \ref{growthintuition} are nice.\end{document}

I get a very nice output
Output 1
test.PNG (40.79 KiB) Viewed 5112 times
.

However, when I copy and paste the above code in the middle of my article, the output is not as nice because, although the caption says figure 1, we can read subfigures 2a and 2b at the top of the figure and the subcaption are below the figures.
Output 2
sample.PNG (58.46 KiB) Viewed 5112 times

Moreover, when I run the code
\begin{figure}[h]\centering\subfloat[Linear projection of tuition prices into ENEM results\label{levelintuition}]{\includegraphics[width=.45\columnwidth]{model1.png}} \quad\subfloat[Correlation between tuition prices and ENEM results\label{growthintuition}]{\includegraphics[width=.45\columnwidth]{model2.png}}\caption{Intuitive explanation of Models \ref{level} and \ref{growth}\label{intuition}}\end{figure}
,

the references are correctly numbered, but the captions are below the figure.
Output 3
sample2.PNG (53.67 KiB) Viewed 5112 times

I wanted the captions to be above the figure. Can it be a problem in my preamble? I think it is, because when I run
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Arsclassica Article% LaTeX Template% Version 1.1 (10/6/14)%% This template has been downloaded from:% http://www.LaTeXTemplates.com%% Original author:% Lorenzo Pantieri (http://www.lorenzopantieri.net) with extensive modifications by:% Vel (vel@latextemplates.com)%% License:% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %----------------------------------------------------------------------------------------%	PACKAGES AND OTHER DOCUMENT CONFIGURATIONS%---------------------------------------------------------------------------------------- \documentclass[10pt, % Main document font sizea4paper, % Paper type, use 'letterpaper' for US Letter paperoneside, % One page layout (no page indentation)%twoside, % Two page layout (page indentation for binding and different headers)headinclude,footinclude, % Extra spacing for the header and footerBCOR5mm, % Binding correction]{scrartcl} \input{structure.tex} % Include the structure.tex file which specified the document structure and layout\usepackage[left=3cm,right=2cm,top=3cm,bottom=2cm]{geometry} \hyphenation{Fortran hy-phen-ation} % Specify custom hyphenation points in words with dashes where you would like hyphenation to occur, or alternatively, don't put any dashes in a word to stop hyphenation altogether %----------------------------------------------------------------------------------------% Packages%---------------------------------------------------------------------------------------- %----------------------------------------------------------------------------------------% Main packages %----------------------------------------------------------------------------------------% \usepackage{cmap}				% Mapear caracteres especiais no PDF% \usepackage{newtxtext}		% Usa a fonte Times New Roman\usepackage{color}				% Permite o uso de textos coloridos% \usepackage{indentfirst}		% Indenta o primeiro parágrafo de cada seção.% \usepackage{microtype} 		% para melhorias de justificação\usepackage{url}				% Para escrever links\usepackage{float}				% Para não deixar figuras voando no texto% \newsubfloat{figure} 			% Subfigures% \usepackage{pbox}% \usepackage[flushleft]{threeparttable} % Aparentemente, isso deveria me ajudar a fazer notas de tabela. %----------------------------------------------------------------------------------------% MATH PACKAGES%---------------------------------------------------------------------------------------- \usepackage{dsfont} %Escreve a função indicadora\usepackage{bm}\theoremstyle{plain} % Define theorem styles here based on the plain style (used for theorems, lemmas, propositions)\newtheorem{model}{Model} %----------------------------------------------------------------------------------------% TABLE PACKAGES%----------------------------------------------------------------------------------------\usepackage{tabulary}\usepackage{enumerate}\usepackage{booktabs}\usepackage{multirow}\usepackage{pdflscape}\usepackage{afterpage}\usepackage{capt-of} %----------------------------------------------------------------------------------------% BIBLIOGRAPHY PACKAGES%---------------------------------------------------------------------------------------- \usepackage{natbib}\bibliographystyle{plainnat} %----------------------------------------------------------------------------------------%	TITLE AND AUTHOR(S)%---------------------------------------------------------------------------------------- \title{\normalfont\spacedallcaps{Title}} % The article title \author{\spacedlowsmallcaps{Author}} % The article author(s) - author affiliations need to be specified in the AUTHOR AFFILIATIONS block \date{May 2015} % An optional date to appear under the author(s) \begin{document}\begin{figure}[h]\centering\caption{Intuitive explanation of Models 1 and 2}\label{intuition}\subfloat[Linear projection of tuition prices into ENEM results\label{levelintuition}]{\includegraphics[width=.45\columnwidth]{model1.png}} \quad\subfloat[Correlation between tuition prices and ENEM results\label{growthintuition}]{\includegraphics[width=.45\columnwidth]{model2.png}}\end{figure}Subfigures \ref{levelintuition} and \ref{growthintuition} are nice.\end{document}

my output is problematic again:
Output 4
test2.PNG (46.72 KiB) Viewed 5112 times

If it helps, I also added my two figures

VitorPossebom
Posts: 4
Joined: Sat May 09, 2015 3:41 am
If sending you my figures files is any way helpful, I also attached them to this message:
Model 1
model1.png (4.87 KiB) Viewed 5112 times

Model 2
model2.png (3.48 KiB) Viewed 5112 times

Johannes_B
Site Moderator
Posts: 4038
Joined: Thu Nov 01, 2012 4:08 pm
I was a bit in a hurry and didn't even provide the hacky solution, as i thought something better would turn up.

After doing some testing, the cause lies at a position i would never have guessed. Let me do a bit of investigating there first.

For now, add \addtocounter{figure}{-1} after your caption. It will step the value one down.

EDIT: KOMA is doing a bit more than i expected there. Ignore the above mentioned hack and use the proper captions=above instead. Please have a look at the follwing two minimal working examples. One uses the more modern package subcaption. Might be a bit more typing, but you have mmore control, are in charge, and it provides hyperref support. Package subfig is known to be a bit buggy there.

\documentclass[captions=figureabove]{scrartcl}\usepackage{subcaption}\begin{document}\begin{figure}	\caption{Intuitive explanation of Models 1 and 2}	\label{intuition}	\centering	\begin{subfigure}{.45\linewidth}		\centering\rule{4cm}{1cm}	\caption{Linear projection of tuition prices into ENEM results\label{levelintuition}} \quad\end{subfigure}\hfill	\begin{subfigure}{.45\linewidth}		\centering\rule{4cm}{1cm}	\caption{Correlation between tuition prices and ENEM results\label{growthintuition}}\end{subfigure}\end{figure}Subfigures \ref{levelintuition} and \ref{growthintuition} are nice.\end{document}

\documentclass[captions=figureabove]{scrartcl}\usepackage{subfig}\usepackage{graphicx}\begin{document}\begin{figure}	\centering	\caption{Intuitive explanation of Models 1 and 2}	\label{intuition}	\subfloat[Linear projection of tuition prices into ENEM results\label{levelintuition}]{\rule{4cm}{1cm}} \quad	\subfloat[Correlation between tuition prices and ENEM results\label{growthintuition}]{\rule{4cm}{1cm}}\end{figure}Subfigures \ref{levelintuition} and \ref{growthintuition} are nice.\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.

VitorPossebom
Posts: 4
Joined: Sat May 09, 2015 3:41 am
Thank you very much, Johannes_B. It worked perfectly now. By the way, it is a very beautiful template.