LaTeX forum ⇒ Articles, Essays, and Journal TemplatesProblem 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

Postby VitorPossebom » Sat May 09, 2015 3:53 am

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.

  1. \begin{figure}[h]
  2. \centering
  3. \subfloat[Linear projection of tuition prices into ENEM results\label{levelintuition}]{\includegraphics[width=.45\columnwidth]{model1.png}} \quad
  4. \subfloat[Correlation between tuition prices and ENEM results\label{growthintuition}]{\includegraphics[width=.45\columnwidth]{model2.png}}
  5. \caption{Intuitive explanation of Models \ref{level} and \ref{growth}\label{intuition}}
  6. \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?
  1. \begin{figure}[h]
  2. \caption{Intuitive explanation of Models \ref{level} and \ref{growth}}
  3. \label{intuition}
  4. \centering
  5. \subfloat[Linear projection of tuition prices into ENEM results\label{levelintuition}]{\includegraphics[width=.45\columnwidth]{model1.png}} \quad
  6. \subfloat[Correlation between tuition prices and ENEM results\label{growthintuition}]{\includegraphics[width=.45\columnwidth]{model2.png}}
  7. \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:

User avatar
Johannes_B
Site Moderator
Posts: 3854
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Sat May 09, 2015 3:06 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.
  1. \documentclass{article}
  2. \usepackage{graphicx}
  3. \usepackage{subfig}
  4. \begin{document}
  5. \begin{figure}[h]
  6. \centering
  7. \caption{Intuitive explanation of Models \ref{level} and \ref{growth}\label{intuition}}
  8. \subfloat[Linear projection of tuition prices into ENEM
  9. results\label{level}]{\includegraphics[width=.45\columnwidth]{example-image-a}}
  10. \quad
  11. \subfloat[Correlation between tuition prices and ENEM
  12. results\label{growth}]{\includegraphics[width=.45\columnwidth]{example-image-b}}
  13. \end{figure}
  14. \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

Postby VitorPossebom » Sat May 09, 2015 4:01 pm

Thank you very much, Johannes_B.

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

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


I get a very nice output
test.PNG
Output 1
test.PNG (40.79 KiB) Viewed 3904 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.
sample.PNG
Output 2
sample.PNG (58.46 KiB) Viewed 3904 times


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

the references are correctly numbered, but the captions are below the figure.
sample2.PNG
Output 3
sample2.PNG (53.67 KiB) Viewed 3904 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
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. % Arsclassica Article
  3. % LaTeX Template
  4. % Version 1.1 (10/6/14)
  5. %
  6. % This template has been downloaded from:
  7. % http://www.LaTeXTemplates.com
  8. %
  9. % Original author:
  10. % Lorenzo Pantieri (http://www.lorenzopantieri.net) with extensive modifications by:
  11. % Vel (vel@latextemplates.com)
  12. %
  13. % License:
  14. % CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
  15. %
  16. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  17.  
  18. %----------------------------------------------------------------------------------------
  19. % PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
  20. %----------------------------------------------------------------------------------------
  21.  
  22. \documentclass[
  23. 10pt, % Main document font size
  24. a4paper, % Paper type, use 'letterpaper' for US Letter paper
  25. oneside, % One page layout (no page indentation)
  26. %twoside, % Two page layout (page indentation for binding and different headers)
  27. headinclude,footinclude, % Extra spacing for the header and footer
  28. BCOR5mm, % Binding correction
  29. ]{scrartcl}
  30.  
  31. \input{structure.tex} % Include the structure.tex file which specified the document structure and layout
  32. \usepackage[left=3cm,right=2cm,top=3cm,bottom=2cm]{geometry}
  33.  
  34. \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
  35.  
  36. %----------------------------------------------------------------------------------------
  37. % Packages
  38. %----------------------------------------------------------------------------------------
  39.  
  40. %----------------------------------------------------------------------------------------
  41. % Main packages
  42. %----------------------------------------------------------------------------------------
  43. % \usepackage{cmap} % Mapear caracteres especiais no PDF
  44. % \usepackage{newtxtext} % Usa a fonte Times New Roman
  45. \usepackage{color} % Permite o uso de textos coloridos
  46. % \usepackage{indentfirst} % Indenta o primeiro parágrafo de cada seção.
  47. % \usepackage{microtype} % para melhorias de justificação
  48. \usepackage{url} % Para escrever links
  49. \usepackage{float} % Para não deixar figuras voando no texto
  50. % \newsubfloat{figure} % Subfigures
  51. % \usepackage{pbox}
  52. % \usepackage[flushleft]{threeparttable} % Aparentemente, isso deveria me ajudar a fazer notas de tabela.
  53.  
  54. %----------------------------------------------------------------------------------------
  55. % MATH PACKAGES
  56. %----------------------------------------------------------------------------------------
  57.  
  58. \usepackage{dsfont} %Escreve a função indicadora
  59. \usepackage{bm}
  60. \theoremstyle{plain} % Define theorem styles here based on the plain style (used for theorems, lemmas, propositions)
  61. \newtheorem{model}{Model}
  62.  
  63. %----------------------------------------------------------------------------------------
  64. % TABLE PACKAGES
  65. %----------------------------------------------------------------------------------------
  66. \usepackage{tabulary}
  67. \usepackage{enumerate}
  68. \usepackage{booktabs}
  69. \usepackage{multirow}
  70. \usepackage{pdflscape}
  71. \usepackage{afterpage}
  72. \usepackage{capt-of}
  73.  
  74. %----------------------------------------------------------------------------------------
  75. % BIBLIOGRAPHY PACKAGES
  76. %----------------------------------------------------------------------------------------
  77.  
  78. \usepackage{natbib}
  79. \bibliographystyle{plainnat}
  80.  
  81. %----------------------------------------------------------------------------------------
  82. % TITLE AND AUTHOR(S)
  83. %----------------------------------------------------------------------------------------
  84.  
  85. \title{\normalfont\spacedallcaps{Title}} % The article title
  86.  
  87. \author{\spacedlowsmallcaps{Author}} % The article author(s) - author affiliations need to be specified in the AUTHOR AFFILIATIONS block
  88.  
  89. \date{May 2015} % An optional date to appear under the author(s)
  90.  
  91. \begin{document}
  92. \begin{figure}[h]
  93. \centering
  94. \caption{Intuitive explanation of Models 1 and 2}
  95. \label{intuition}
  96. \subfloat[Linear projection of tuition prices into ENEM results\label{levelintuition}]{\includegraphics[width=.45\columnwidth]{model1.png}} \quad
  97. \subfloat[Correlation between tuition prices and ENEM results\label{growthintuition}]{\includegraphics[width=.45\columnwidth]{model2.png}}
  98. \end{figure}
  99. Subfigures \ref{levelintuition} and \ref{growthintuition} are nice.
  100. \end{document}


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


If it helps, I also added my two figures

VitorPossebom
Posts: 4
Joined: Sat May 09, 2015 3:41 am

Postby VitorPossebom » Sat May 09, 2015 4:04 pm

If sending you my figures files is any way helpful, I also attached them to this message:
Model 1
model1.png
model1.png (4.87 KiB) Viewed 3904 times


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

User avatar
Johannes_B
Site Moderator
Posts: 3854
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Sun May 10, 2015 12:57 am

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.

  1. \documentclass[captions=figureabove]{scrartcl}
  2. \usepackage{subcaption}
  3. \begin{document}
  4. \begin{figure}
  5. \caption{Intuitive explanation of Models 1 and 2}
  6. \label{intuition}
  7. \centering
  8. \begin{subfigure}{.45\linewidth}
  9. \centering
  10. \rule{4cm}{1cm}
  11. \caption{Linear projection of tuition prices into ENEM results\label{levelintuition}} \quad
  12. \end{subfigure}\hfill
  13. \begin{subfigure}{.45\linewidth}
  14. \centering
  15. \rule{4cm}{1cm}
  16. \caption{Correlation between tuition prices and ENEM results\label{growthintuition}}
  17. \end{subfigure}
  18. \end{figure}
  19. Subfigures \ref{levelintuition} and \ref{growthintuition} are nice.
  20. \end{document}

  1. \documentclass[captions=figureabove]{scrartcl}
  2. \usepackage{subfig}
  3. \usepackage{graphicx}
  4. \begin{document}
  5. \begin{figure}
  6. \centering
  7. \caption{Intuitive explanation of Models 1 and 2}
  8. \label{intuition}
  9. \subfloat[Linear projection of tuition prices into ENEM results\label{levelintuition}]{\rule{4cm}{1cm}} \quad
  10. \subfloat[Correlation between tuition prices and ENEM results\label{growthintuition}]{\rule{4cm}{1cm}}
  11. \end{figure}
  12. Subfigures \ref{levelintuition} and \ref{growthintuition} are nice.
  13. \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

Postby VitorPossebom » Sun May 10, 2015 4:37 am

Thank you very much, Johannes_B. It worked perfectly now. By the way, it is a very beautiful template.


Return to “Articles, Essays, and Journal Templates”

Who is online

Users browsing this forum: No registered users and 1 guest