figure_demo.tex

Download
latex 532 lines 16.0 KB
  1% ============================================================================
  2% figure_demo.tex - Figures, Graphics, and Image Inclusion
  3% ============================================================================
  4% This document demonstrates how to include figures, use subfigures, add
  5% captions and labels, and cross-reference figures in LaTeX.
  6%
  7% Compilation:
  8%   pdflatex figure_demo.tex (run twice for cross-references)
  9%
 10% Note: This example uses 'example-image' placeholder images that are
 11% provided by the mwe package. For your own documents, replace these
 12% with actual image files.
 13% ============================================================================
 14
 15\documentclass[11pt]{article}
 16
 17\usepackage[margin=1in]{geometry}
 18\usepackage[T1]{fontenc}
 19\usepackage{lmodern}
 20
 21% Graphics packages
 22\usepackage{graphicx}      % For \includegraphics
 23\usepackage{float}         % For H placement specifier
 24\usepackage{caption}       % Enhanced caption formatting
 25\usepackage{subcaption}    % For subfigures
 26
 27% Example images (part of mwe package, automatically available)
 28% For your documents, you'll use actual image files like:
 29% \includegraphics{path/to/your/image.png}
 30
 31% Colors for boxes
 32\usepackage{xcolor}
 33\usepackage{tcolorbox}
 34
 35% Hyperlinks
 36\usepackage{hyperref}
 37\hypersetup{colorlinks=true, linkcolor=blue}
 38
 39% Set default graphics path (optional)
 40% \graphicspath{{./images/}{./figures/}}
 41
 42\title{Figures and Graphics in \LaTeX}
 43\author{Graphics Department}
 44\date{\today}
 45
 46\begin{document}
 47
 48\maketitle
 49
 50\tableofcontents
 51
 52\listoffigures
 53
 54\section{Introduction}
 55
 56This document demonstrates how to include and manage figures in \LaTeX{}
 57documents. Figures are floating environments that can contain images, diagrams,
 58or other graphical content.
 59
 60% ============================================================================
 61\section{Basic Figure Inclusion}
 62% ============================================================================
 63
 64\subsection{The \texttt{figure} Environment}
 65
 66The basic structure for including a figure is:
 67
 68\begin{verbatim}
 69\begin{figure}[placement]
 70    \centering
 71    \includegraphics[options]{filename}
 72    \caption{Caption text}
 73    \label{fig:label}
 74\end{figure}
 75\end{verbatim}
 76
 77\subsection{Simple Figure Example}
 78
 79Here is a simple figure using a placeholder image:
 80
 81\begin{figure}[h]
 82    \centering
 83    \includegraphics[width=0.5\textwidth]{example-image}
 84    \caption{A simple example figure with default placeholder image}
 85    \label{fig:simple}
 86\end{figure}
 87
 88Figure~\ref{fig:simple} shows a basic example. The \verb|[h]| placement
 89specifier suggests placing the figure "here" if possible.
 90
 91\subsection{Placement Specifiers}
 92
 93LaTeX uses placement specifiers to control where figures appear:
 94
 95\begin{table}[h]
 96\centering
 97\begin{tabular}{cl}
 98\hline
 99Specifier & Meaning \\
100\hline
101\texttt{h} & Here (approximately) \\
102\texttt{t} & Top of page \\
103\texttt{b} & Bottom of page \\
104\texttt{p} & On a separate page of floats \\
105\texttt{!} & Override LaTeX's placement rules \\
106\texttt{H} & Exactly here (requires \texttt{float} package) \\
107\hline
108\end{tabular}
109\end{table}
110
111You can combine specifiers, e.g., \verb|[htbp]| tries here, then top, then
112bottom, then a float page.
113
114% ============================================================================
115\section{Sizing and Scaling Images}
116% ============================================================================
117
118\subsection{Width-Based Sizing}
119
120The most common approach is to specify width relative to text width:
121
122\begin{figure}[h]
123    \centering
124    \includegraphics[width=0.3\textwidth]{example-image-a}
125    \caption{Image scaled to 30\% of text width}
126    \label{fig:width30}
127\end{figure}
128
129\begin{figure}[h]
130    \centering
131    \includegraphics[width=0.7\textwidth]{example-image-b}
132    \caption{Image scaled to 70\% of text width}
133    \label{fig:width70}
134\end{figure}
135
136\subsection{Height-Based Sizing}
137
138You can also specify height:
139
140\begin{figure}[h]
141    \centering
142    \includegraphics[height=3cm]{example-image-c}
143    \caption{Image with fixed height of 3cm}
144    \label{fig:height}
145\end{figure}
146
147\subsection{Scale Factor}
148
149Or use a scale factor:
150
151\begin{figure}[h]
152    \centering
153    \includegraphics[scale=0.5]{example-image}
154    \caption{Image scaled to 50\% of original size}
155    \label{fig:scale}
156\end{figure}
157
158\subsection{Maintaining Aspect Ratio}
159
160By default, specifying only width or height maintains the aspect ratio.
161To specify both (distorting the image):
162
163\begin{verbatim}
164\includegraphics[width=5cm, height=3cm]{image}
165\end{verbatim}
166
167To maintain aspect ratio while fitting in a box:
168
169\begin{verbatim}
170\includegraphics[width=5cm, height=3cm, keepaspectratio]{image}
171\end{verbatim}
172
173% ============================================================================
174\section{Captions and Labels}
175% ============================================================================
176
177\subsection{Caption Placement}
178
179Captions can appear above or below the figure:
180
181\begin{figure}[h]
182    \centering
183    \caption{Caption above the image}
184    \includegraphics[width=0.4\textwidth]{example-image}
185    \label{fig:caption_top}
186\end{figure}
187
188\begin{figure}[h]
189    \centering
190    \includegraphics[width=0.4\textwidth]{example-image}
191    \caption{Caption below the image (more common)}
192    \label{fig:caption_bottom}
193\end{figure}
194
195Convention: captions typically appear below figures but above tables.
196
197\subsection{Long Captions}
198
199For long captions, use the optional short caption for the list of figures:
200
201\begin{figure}[h]
202    \centering
203    \includegraphics[width=0.5\textwidth]{example-image}
204    \caption[Short caption for LOF]{This is a very long caption that provides
205    detailed information about the figure, including methodology, data sources,
206    and interpretation. It may span multiple lines and include technical details
207    that would clutter the List of Figures. The short caption in brackets
208    appears in the LOF instead.}
209    \label{fig:long_caption}
210\end{figure}
211
212\subsection{Cross-Referencing}
213
214Reference figures using \verb|\ref| or \verb|\pageref|:
215
216\begin{itemize}
217    \item Figure~\ref{fig:simple} shows a simple example
218    \item See Figure~\ref{fig:width70} on page~\pageref{fig:width70}
219    \item As demonstrated in Figures~\ref{fig:caption_top} and~\ref{fig:caption_bottom}
220\end{itemize}
221
222% ============================================================================
223\section{Subfigures}
224% ============================================================================
225
226\subsection{Basic Subfigures}
227
228The \texttt{subcaption} package provides the \texttt{subfigure} environment:
229
230\begin{figure}[h]
231    \centering
232    \begin{subfigure}{0.45\textwidth}
233        \centering
234        \includegraphics[width=\textwidth]{example-image-a}
235        \caption{First subfigure}
236        \label{fig:sub_a}
237    \end{subfigure}
238    \hfill
239    \begin{subfigure}{0.45\textwidth}
240        \centering
241        \includegraphics[width=\textwidth]{example-image-b}
242        \caption{Second subfigure}
243        \label{fig:sub_b}
244    \end{subfigure}
245    \caption{Two subfigures side by side}
246    \label{fig:two_subfigs}
247\end{figure}
248
249Reference individual subfigures: Figure~\ref{fig:sub_a} shows the first image,
250while Figure~\ref{fig:sub_b} shows the second. Together, they form
251Figure~\ref{fig:two_subfigs}.
252
253\subsection{Multiple Rows of Subfigures}
254
255\begin{figure}[h]
256    \centering
257    \begin{subfigure}{0.3\textwidth}
258        \centering
259        \includegraphics[width=\textwidth]{example-image-a}
260        \caption{Subfigure A}
261        \label{fig:multi_a}
262    \end{subfigure}
263    \hfill
264    \begin{subfigure}{0.3\textwidth}
265        \centering
266        \includegraphics[width=\textwidth]{example-image-b}
267        \caption{Subfigure B}
268        \label{fig:multi_b}
269    \end{subfigure}
270    \hfill
271    \begin{subfigure}{0.3\textwidth}
272        \centering
273        \includegraphics[width=\textwidth]{example-image-c}
274        \caption{Subfigure C}
275        \label{fig:multi_c}
276    \end{subfigure}
277
278    \vspace{0.5cm}
279
280    \begin{subfigure}{0.45\textwidth}
281        \centering
282        \includegraphics[width=\textwidth]{example-image}
283        \caption{Subfigure D}
284        \label{fig:multi_d}
285    \end{subfigure}
286    \hfill
287    \begin{subfigure}{0.45\textwidth}
288        \centering
289        \includegraphics[width=\textwidth]{example-image-golden}
290        \caption{Subfigure E}
291        \label{fig:multi_e}
292    \end{subfigure}
293
294    \caption{Multiple subfigures arranged in rows}
295    \label{fig:multi_subfigs}
296\end{figure}
297
298Figure~\ref{fig:multi_subfigs} contains five subfigures arranged in two rows.
299
300\subsection{Shared Subfigure Captions}
301
302You can also use \texttt{subcaptionbox} for more compact code:
303
304\begin{figure}[h]
305    \centering
306    \subcaptionbox{First image\label{fig:box_a}}
307        {\includegraphics[width=0.3\textwidth]{example-image-a}}
308    \hfill
309    \subcaptionbox{Second image\label{fig:box_b}}
310        {\includegraphics[width=0.3\textwidth]{example-image-b}}
311    \hfill
312    \subcaptionbox{Third image\label{fig:box_c}}
313        {\includegraphics[width=0.3\textwidth]{example-image-c}}
314    \caption{Three images using subcaptionbox}
315    \label{fig:subcaptionbox}
316\end{figure}
317
318% ============================================================================
319\section{Advanced Figure Formatting}
320% ============================================================================
321
322\subsection{Rotating Images}
323
324Rotate images using the \texttt{angle} option:
325
326\begin{figure}[h]
327    \centering
328    \includegraphics[width=0.3\textwidth, angle=45]{example-image}
329    \caption{Image rotated 45 degrees}
330    \label{fig:rotated}
331\end{figure}
332
333\subsection{Clipping and Trimming}
334
335Crop images using \texttt{trim} and \texttt{clip}:
336
337\begin{verbatim}
338\includegraphics[trim={left bottom right top}, clip, width=5cm]{image}
339\end{verbatim}
340
341\subsection{Framed Figures}
342
343Add a frame around figures:
344
345\begin{figure}[h]
346    \centering
347    \fbox{\includegraphics[width=0.4\textwidth]{example-image}}
348    \caption{Image with a frame}
349    \label{fig:framed}
350\end{figure}
351
352\subsection{Custom Frame Thickness}
353
354\begin{figure}[h]
355    \centering
356    \setlength{\fboxrule}{3pt}
357    \fbox{\includegraphics[width=0.4\textwidth]{example-image}}
358    \caption{Image with thick frame (3pt)}
359    \label{fig:thick_frame}
360\end{figure}
361
362% ============================================================================
363\section{Figure Positioning Tips}
364% ============================================================================
365
366\begin{tcolorbox}[colback=blue!5, colframe=blue!75!black, title=Best Practices]
367\begin{enumerate}
368    \item Use \verb|[htbp]| as default placement specifiers
369    \item Prefer \verb|\textwidth| for width scaling (responsive to document layout)
370    \item Always include \verb|\centering| before \verb|\includegraphics|
371    \item Place \verb|\label| after \verb|\caption| for correct referencing
372    \item Use vector formats (PDF, SVG) over raster (PNG, JPG) when possible
373    \item Keep figure files organized in a separate directory
374\end{enumerate}
375\end{tcolorbox}
376
377\subsection{Forcing Exact Placement}
378
379To force a figure to appear exactly at the current position (not recommended
380for most cases):
381
382\begin{figure}[H]
383    \centering
384    \includegraphics[width=0.3\textwidth]{example-image}
385    \caption{Figure with [H] placement (exactly here, not floating)}
386    \label{fig:exact_here}
387\end{figure}
388
389The \verb|[H]| specifier requires the \texttt{float} package and prevents
390the figure from floating. This can lead to large whitespace gaps.
391
392\subsection{Preventing Figures from Moving Too Far}
393
394Control float placement globally in the preamble:
395
396\begin{verbatim}
397\setcounter{topnumber}{2}         % Max floats at top
398\setcounter{bottomnumber}{2}      % Max floats at bottom
399\setcounter{totalnumber}{4}       % Max floats per page
400\renewcommand{\topfraction}{0.7}  % Max fraction of page for top floats
401\renewcommand{\bottomfraction}{0.3}
402\renewcommand{\textfraction}{0.2} % Min fraction for text
403\renewcommand{\floatpagefraction}{0.7} % Min for float-only page
404\end{verbatim}
405
406% ============================================================================
407\section{Supported Image Formats}
408% ============================================================================
409
410\subsection{Format Recommendations}
411
412\begin{table}[h]
413\centering
414\caption{Image format recommendations for LaTeX}
415\begin{tabular}{llp{6cm}}
416\hline
417Format & Type & Best Use \\
418\hline
419PDF & Vector & Diagrams, plots, line art \\
420EPS & Vector & Legacy format (still widely used) \\
421PNG & Raster & Screenshots, photos with transparency \\
422JPG & Raster & Photographs (no transparency) \\
423SVG & Vector & Web graphics (convert to PDF first) \\
424\hline
425\end{tabular}
426\end{table}
427
428\subsection{Compilation Differences}
429
430\begin{itemize}
431    \item \textbf{pdflatex}: Supports PDF, PNG, JPG (not EPS directly)
432    \item \textbf{latex + dvips + ps2pdf}: Supports EPS (not PDF directly)
433    \item \textbf{lualatex/xelatex}: Supports PDF, PNG, JPG
434\end{itemize}
435
436For maximum compatibility, convert EPS to PDF:
437\begin{verbatim}
438epstopdf myimage.eps
439\end{verbatim}
440
441% ============================================================================
442\section{Complete Example}
443% ============================================================================
444
445Here is a complete example combining multiple techniques:
446
447\begin{figure}[htbp]
448    \centering
449
450    \begin{subfigure}{0.48\textwidth}
451        \centering
452        \includegraphics[width=\textwidth]{example-image-a}
453        \caption{Original experimental data showing baseline measurements}
454        \label{fig:complete_original}
455    \end{subfigure}
456    \hfill
457    \begin{subfigure}{0.48\textwidth}
458        \centering
459        \includegraphics[width=\textwidth]{example-image-b}
460        \caption{Processed results after applying the proposed algorithm}
461        \label{fig:complete_processed}
462    \end{subfigure}
463
464    \vspace{0.5cm}
465
466    \begin{subfigure}{0.6\textwidth}
467        \centering
468        \includegraphics[width=\textwidth]{example-image-c}
469        \caption{Comparative analysis demonstrating 25\% improvement over
470        baseline}
471        \label{fig:complete_analysis}
472    \end{subfigure}
473
474    \caption[Complete experimental results]{Complete experimental results.
475    \textbf{(a)} Shows the original data collected from the experiment.
476    \textbf{(b)} Displays the processed output. \textbf{(c)} Presents the
477    comparative analysis with statistical significance ($p < 0.05$).}
478    \label{fig:complete}
479\end{figure}
480
481As shown in Figure~\ref{fig:complete}, the experimental procedure yielded
482significant results. The original data (Figure~\ref{fig:complete_original})
483was processed to obtain Figure~\ref{fig:complete_processed}, and the
484comparative analysis in Figure~\ref{fig:complete_analysis} confirms our
485hypothesis.
486
487% ============================================================================
488\section{Conclusion}
489% ============================================================================
490
491This document has demonstrated:
492
493\begin{itemize}
494    \item Basic figure inclusion with \verb|\includegraphics|
495    \item Sizing and scaling options
496    \item Captions and cross-referencing
497    \item Creating subfigures with \texttt{subcaption}
498    \item Advanced formatting (rotation, frames, clipping)
499    \item Placement control and best practices
500    \item Image format recommendations
501\end{itemize}
502
503For your own documents, remember to:
504\begin{enumerate}
505    \item Replace \texttt{example-image} with your actual image files
506    \item Set \verb|\graphicspath| to your image directory
507    \item Use vector formats (PDF) for diagrams and plots
508    \item Always compile twice to resolve cross-references
509\end{enumerate}
510
511\end{document}
512
513% ============================================================================
514% COMPILATION NOTES
515% ============================================================================
516% 1. Compile with: pdflatex figure_demo.tex (twice for cross-references)
517%
518% 2. For your own images, create a structure like:
519%    project/
520%    ├── document.tex
521%    └── images/
522%        ├── figure1.pdf
523%        ├── figure2.png
524%        └── photo.jpg
525%
526% 3. Then add to preamble:
527%    \graphicspath{{./images/}}
528%
529% 4. And use:
530%    \includegraphics{figure1}  (no extension needed)
531% ============================================================================