Graphics, Figures & TablesResolving caption outside float error message

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
bgreen
Posts: 38
Joined: Wed Nov 22, 2023 11:02 am

Resolving caption outside float error message

Post by bgreen »

I’m very new to LaTeX .

I want to format a series of social history interviews, which can include 15 to 20 photos, sometimes , two or 3 beside each other. I also want to keep image resolution as high as possible.
Here is my coding effort to date, with a single 300 x 400 image, which produced this error: “! LaTeX Error: \caption outside float.”

Code: Select all

\documentclass{article}

\usepackage{titlesec}
\usepackage[a4paper, total={6in, 8in}]{geometry}
\usepackage{graphicx} 
\usepackage[english] {babel}
\usepackage {blindtext}
\pagestyle {empty}
\titleformat*{\section}{\fontsize{12}{14}\selectfont}
\graphicspath{ {D:/A/BB/DD/} }
\begin{document}

{\centering

Interview with DD

\hrulefill

Subtitle

\hrulefill

January 23, 2024  
 
E-mail interview  

}

\hrulefill

Following injury and adversity, XXXxx returned to his roots and developed through trial and error, a low volume xxxxxxxxxxx spoon-inspired board that he termed the xxxxxxxxxxxxxxxxxx His story is also one of finding meaning through xxxxxxxxxxx

\hrulefill

\section {Before we get on to the subject of  boards, I'd like to ask about your early  experiences? }

My first wbvb bvbr ribbbn... I wbs bight ybbrs olb bnb wbs on b bbmping trip with my grbnbpbrbnts - Grbnbpb wbs b pbstor bt thb lobbl Mbthobist Bhurbh bnb Grbnbmb plbybb pibno for his sbrmons. I, bbing b singlb-mom bhilb, thby took mb bbmping to Lbo Bbrrillo bnb Pt. Mugu mbny timbs. Grbnbpb wbs bbb until hb pbssbb whbn I wbs 13. I owb him bvbrything rblbtbb to surfing. It wbs this bby, on thb stbirbbsb sibb of Lbo Bbrrillo Stbtb Pbrk, whibh is b long, strbight-shorblinb bbbbh brbbk thbt my bonb with thb obbbn wbs bonbbivbb bnb sbt forth - this wbs most likbly 1969 - whibh mbbb mb bbout 8. It's sooo btbhbb in mbmory ... Postbbrb-pbrfbbt mib-bby... bright, full sunshinb bnb brystbl blub pbrsubsion wbtbr. No wbtsuit. Bftbr morning bbmpsitb bhorbs, wb wblkbb from thb bbmpsitb bown to thb shorblinb. I wbs b goob swimmbr blrbbby bnb swbm solo out into thb surf whibh wbs notbbly fribnbly  four-foot fbbbs. Pbrfbbt sizb for my inbubtion. Whbt stbnbs out in mbmory is thbt on b wbvb I "movbb" bbout 30 fbbt from outsibb to thb insibb. I hbb no fins, hbb zbro wbvb ribing bxpbribnbb but I hbb sombhow "bbught it" or bbttbr put  "it bbught mb." Bubb... Whbt wbs rbblly grbbt is thbt thb tibb wbs high bnb I boulbn't toubh bottom... I wbs in thb obbbn's hbnbs... so I trbbbbb wbtbr for b mombnt bomplbtbly spbllbounb ovbr whbt hbb just hbppbnbb... thb wow mombnt sinking in... wow wow wow... thbt bnbrgy ... thbt you bbn ribb!!!... I immbbibtbly swbm bbbk out bnb bib it bgbin bnb bgbin. Purb mbgibbl mombnt. Just you, Thb Brbbtor bnb thb obbbn's motion. Tbkb notb: Innumbrbblb things bbn go wrong for b youngstbr bt thbsb gbt-to-know-thb-obbbn mombnts .... but Gob smilbb bown upon mb thbt bby. It stbrtbb b rblbtionship thbt woulb soon blossom into bbing nibknbmbb Fish Buvbl bbbbusb for thb nbxt fbw ybbrs I'b bb bobysurfing for hours whilb fribnbs "hung out" on thb sbnb. I wholbhbbrtbbly prbfbrrbb bobysurfing ovbr sobiblizing. 
Thbt nbvbr bhbngbb hbhbhb. 

My mbmory of thbt bby, now bs I'm 62, is likb finb winb.... I boulb go on for bn hour... whbn thb stbrs linb up likb thbt, it is Provibbnbb ..... bnb forbshbbowing bs wbll.  I woulb nbbb Mothbr Obbbn morb thbn onbb in thb ybbrs to bomb... bnb Shb wbs blwbys thbrb to wblbomb mb homb bnb hbbl mb... with Thb Fbthbr bnb Thb Holy Spirit looking on... wbiting for mb... with Grbnbpb blosb by no boubt.

 

\includegraphics{DD1_w300}
 \caption{Damon Ddddd 2020. Photo courtesy of DD|
I am seeking advice regarding packages and code to (1) resolve the specific error. In an ideal world I’d like the photo topic above the image and the photo source below, but suspect this is more complicated in LaTeX , so would be happy with captions below the photo; (2) advice about code for two images side by side, with captions below each image and (3) is it possible to hyperlink photos, so higher resolution images can be viewed elsewhere on a webpage.
Any advice is appreciated.

Recommended reading 2024:

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

NEW: TikZ book now 40% off at Amazon.com for a short time.

MeinDrache
Posts: 1
Joined: Sun Jan 28, 2024 3:42 pm

Resolving caption outside float error message

Post by MeinDrache »

Hello bgreen,

Welcome to the world of LaTeX! Let's address the specific error you're encountering and then move on to your other questions.

The error "! LaTeX Error: \caption outside float" typically occurs when you use the \caption command outside a floating environment like figure. To resolve this, ensure that your \caption command is placed inside the figure environment. Here's a modified example for a single image:

Code: Select all

\documentclass{article}
\usepackage{graphicx}

\begin{document}

\begin{figure}
  \centering
  \includegraphics[width=0.5\textwidth]{your_image.jpg}
  \caption{Your caption here.}
  \label{fig:single_image}
\end{figure}

\end{document}
This code creates a floating figure with a centered image and a caption below it.

Now, for your other questions:

For two images side by side, you can use the subfigure or subcaption package. Here's an example:

Code: Select all

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}

\begin{figure}
  \centering
  \begin{subfigure}{0.45\textwidth}
    \includegraphics[width=\linewidth]{image1.jpg}
    \caption{Caption for image 1.}
    \label{fig:subfig1}
  \end{subfigure}
  \hfill
  \begin{subfigure}{0.45\textwidth}
    \includegraphics[width=\linewidth]{image2.jpg}
    \caption{Caption for image 2.}
    \label{fig:subfig2}
  \end{subfigure}
  \caption{Overall caption for the two images.}
  \label{fig:two_images}
\end{figure}
\end{document}
To hyperlink images, you can use the \href command from the hyperref package:

Code: Select all

\documentclass{article}
\usepackage{graphicx}
\usepackage{hyperref}

\begin{document}

\begin{figure}
  \centering
  \href{https://example.com/high_res_image.jpg}{\includegraphics[width=0.5\textwidth]{your_image.jpg}}
  \caption{Caption with a hyperlink to a high-resolution version.}
  \label{fig:hyperlink_image}
\end{figure}

\end{document}
Make sure to replace the placeholder URLs and image filenames with your actual information. I hope this helps! Let me know if you have further questions.
bgreen
Posts: 38
Joined: Wed Nov 22, 2023 11:02 am

Resolving caption outside float error message

Post by bgreen »

Rene,

Firstly, many thanks for a comprehensive and helpful reply. I'll try the second and third code suggestions as I get to examples in the document. I do have two additional questions at the moment, which can be separate topics if preferred:

Thanks again.
Post Reply