1

I was trying to reference some pictures in my document but something went wrong. Here is the code :

main.tex:

\documentclass[french, 12pt, twoside, openany]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{subfig}

% Telling the subfig package the desired caption positions
\captionsetup[figure]{position=t}
\captionsetup[subfigure]{position=b}
\usepackage{amsmath}
\usepackage{fancyhdr}
\usepackage{float}
\usepackage[french]{babel}
\usepackage[acronym,nonumberlist]{glossaries}
\usepackage{pdflscape}
\usepackage{adjustbox}
\usepackage{geometry}
\usepackage[hidelinks,linktoc=all]{hyperref} % Modification ici

\geometry{
  a4paper,
  left=20mm,
  right=20mm,
  top=20mm,
  bottom=20mm
}

\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE]{\slshape Somebody}
\fancyhead[LO]{\slshape Somewhere}

\input{glossaire}
\makenoidxglossaries

\begin{document}

\setcounter{page}{1}
\thispagestyle{empty}

\begin{center}
\large\bfseries Somwhere\\2024 -- 2027\vfill

{\Large\bfseries Something}\\[1cm]
{\Large\bfseries Something}\\[6cm]

{\Large\bfseries Somebody}\\[1cm]

{\large\bfseries Somebody\\
{\large\bfseries Somebody}

\vfill
\includegraphics[height=2.5cm]{figures/logo-ur1-gris-jpg.jpeg}\hfill
\includegraphics[height=2cm]{figures/logo_irmar.png}
\includegraphics[width=3cm, height=2.5cm]{figures/ecobio.jpeg}
\end{center}

\setcounter{page}{1}

\clearpage
\thispagestyle{empty}
\mbox{}

\frontmatter % Pages en chiffres romains

\include{Remerciment}

\tableofcontents

\include{Introduction}
\clearpage

\printnoidxglossary[title=Glossaire]
\clearpage

\mainmatter % Pages en chiffres arabes à partir de l'analyse des données

\include{Chapitre1}
\clearpage

\include{Chapitre2}
\clearpage

\bibliographystyle{plain}
\bibliography{rapport}

\end{document}

referencing part:

\chapter{Analyse des données}
\thispagestyle{plain}


SEND HELP

    \begin{figure}
    \centering
        \caption{picture 1}
        \label{fig:smth}
        \subfloat[picture 2]{
            \includegraphics[width=0.47\textwidth]{example-image}
            \label{img:first}
        }
        \subfloat[picture 2]{
            \includegraphics[width=0.47\textwidth]{example-image}
        \label{img:second}
        }
    \end{figure}

Here is the output : First page Second page

Firstly, I would like to have those to pictures and the text under it, on the same page. It would be lovely if the reference could be 1.1(a) rather than 1.1a. I thank anybody trying to help in advance.

2
  • 1
    Off-topic: The geometry package options left=20mm, right=20mm, top=20mm, bottom=20mm may be expressed more succinctly as margin=20mm.
    – Mico
    Commented Jul 9 at 3:00
  • 1
    please post the example in a form that people can use. not referencing files that are not available. note all the \clearpage after \include do nothing as \include always forces clearpage anyway, and all but the first of \Large\bfseries do nothing as they are in the scope of the first one. Commented Jul 9 at 8:21

1 Answer 1

2

I would like to have those [two] pictures and the text ["SEND HELP"] on the same page. It would be lovely if the [cross-]reference could be 1.1(a) rather than 1.1a.

To address the first issue, it suffices to change \begin{figure} to \begin{figure}[ht]; \begin{figure}[htbp] will do too.

To fix the second issue, section 2.2.2, "The \subref command", of the user guide of the subfig package explains -- admittedly somewhat tersely! -- that (a) the options listofformat and subrefformat need to be set to the value parens in the \captionsetup[subfigure]{...} directive and (b) you need to use \subref rather than ref to generate the formatted cross-reference.

The following example distils the OP's code to the minimum needed to address the posting's two stated objectives.

enter image description here

\documentclass[french, 12pt, twoside, openany]{book}
%\usepackage[utf8]{inputenc} % that's the default nowadays
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage[a4paper,margin=20mm]{geometry}
\usepackage{graphicx}

\usepackage{subfig}
\captionsetup[figure]{position=t}
\captionsetup[subfigure]{position=b,
                         listofformat=parens, % <-- new
                         subrefformat=parens  % <-- new
                        }

\usepackage{amsmath}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[LE,RO]{\thepage}
\fancyhead[RE]{\slshape Somebody}
\fancyhead[LO]{\slshape Somewhere}

\usepackage[hidelinks,linktoc=all]{hyperref} 

\begin{document}


\chapter{Analyse des données}

SEND HELP

\begin{figure}[htbp] % '[ht]' is new
\caption{picture 1}
\label{fig:smth}
        
  \subfloat[picture a]{% <-- the '%' symbol is important
    \includegraphics[width=0.47\textwidth]{example-image-a}
    \label{img:first}
  }%
  \hfill % maximize the separation between the images
  \subfloat[picture b]{% <-- the '%' symbol is important
    \includegraphics[width=0.47\textwidth]{example-image-b}
    \label{img:second}
  }
\end{figure}

Cross-references to figure \ref{fig:smth} and 
to subfigures \subref{img:first} and \subref{img:second}.

\end{document}
1
  • 1
    @Gigi - In your initial write-up, you "only" required the text and the figures to be on the same page. If you additionally want to place the figure above the text, I suggest you rearrange the document's contents so that the figure material comes *before" the associated text. Of course, if you want the figure material to be placed at the very top of the page, I suggest you change [ht] to just [t].
    – Mico
    Commented Jul 9 at 13:07

You must log in to answer this question.

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