How to add subfigure in Latex

时间:2022-06-19
本文章向大家介绍How to add subfigure in Latex,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

How to add subfigure in Latex

1 minute read

TABLE OF CONTENTS

In research articles, we need to add subfigures often. To create subfigure in latex, you can use both begin{minipage}...end{minipage} and begin{subfigure}...end{subfigure} block to insert subfigures or sub-images. Subfigurs are generally inserted horizontally in one or multiple rows. Here, some example codes with output screenshots are provided in the following.

Add subfigures horizontally

The following code puts two subfigures in a figure portion-

usepackage{subcaption}
begin{figure}[ht]
begin{subfigure}{.5textwidth}
  centering
  % include first image
  includegraphics[width=.8linewidth]{image_file_name}  
  caption{Put your sub-caption here}
  label{fig:sub-first}
end{subfigure}
begin{subfigure}{.5textwidth}
  centering
  % include second image
  includegraphics[width=.8linewidth]{image_file_name}  
  caption{Put your sub-caption here}
  label{fig:sub-second}
end{subfigure}
caption{Put your caption here}
label{fig:fig}
end{figure}

So, applying the code the output should look like this -

Here is another code to do the same. Only difference is, this time we are using begin{minipage}...end{minipage} instead of begin{subfigure}...end{subfigure} block.

begin{figure}[ht]
  subfloat[Percentage storage utilization]{
	begin{minipage}[c][1width]{
	   0.3textwidth}
	   centering
	   includegraphics[width=1textwidth]{image_file_name}
	end{minipage}}
 hfill 	
  subfloat[standard deviation]{
	begin{minipage}[c][1width]{
	   0.3textwidth}
	   centering
	   includegraphics[width=1.1textwidth]{image_file_name}
	end{minipage}}
 hfill	
  subfloat[execution time]{
	begin{minipage}[c][1width]{
	   0.3textwidth}
	   centering
	   includegraphics[width=1.2textwidth]{image_file_name}
	end{minipage}}
caption{}
end{figure}

So, applying the code the output should look like this-

Add multiple subfigures in multiple rows

Multiple subfigures can be put in multiple rows by adding a newline after one row is complete. For example, if you have four figures and you want to put them in 2x2 style, put newline after two subfigures which will be placed in the first rwo. The command will create a new row for rest of the subfigures.

begin{document}
begin{figure}
begin{subfigure}{.5textwidth}
  centering
  % include first image
  includegraphics[width=.8linewidth]{log_demo1.png}  
  caption{Put your sub-caption here}
  label{fig:sub-first}
end{subfigure}
begin{subfigure}{.5textwidth}
  centering
  % include second image
  includegraphics[width=.8linewidth]{log_demo2.png}  
  caption{Put your sub-caption here}
  label{fig:sub-second}
end{subfigure}

newline

begin{subfigure}{.5textwidth}
  centering
  % include third image
  includegraphics[width=.8linewidth]{log_demo1.png}  
  caption{Put your sub-caption here}
  label{fig:sub-third}
end{subfigure}
begin{subfigure}{.5textwidth}
  centering
  % include fourth image
  includegraphics[width=.8linewidth]{log_demo2.png}  
  caption{Put your sub-caption here}
  label{fig:sub-fourth}
end{subfigure}
caption{Put your caption here}
label{fig:fig}
end{figure}

So, applying the code the output should look like this-