
Python スクリプトを記述し、\subcaption を使用して複数の画像を配置する LaTeX スクリプトを生成します。コア コード:
....
\usepackage{subcaption}
....
\begin{figure}
\begin{subfigure}[31]{0.2\textwidth}
\includegraphics[width = \textwidth, height = \textwidth]
{ly1_31.jpg}
\caption{Channel 31}
\label{fig: 31}
\end{subfigure}
\begin{subfigure}[97]{0.2\textwidth}
\includegraphics[width = \textwidth, height = \textwidth]
{ly1_97.jpg}
\caption{Channel 97}
\label{fig: 97}
\end{subfigure}
.......
\caption{ Features maps manually selected from 512 maps }
\label{fig}
\end{figure}
サブ図のコードは、チャンネル番号と Python によって生成されたファイル名を除いて同じです。ここには 14 個の .jpg 画像があります。最後の行の画像間の空白の幅は、前の 3 行よりも明らかに短くなっています (最後の行はいっぱいではありません)。そのスペースの幅を制御するにはどうすればよいでしょうか。
また、5 行目以降に設定したのに、行がいっぱいのときに 1 行に 4 つの画像しか表示されないのは、空白の幅が原因かどうかもわかりません。{0.2\textwidth}
(そうだと思いますが、詳しい説明はありますか?)
最後の質問はかなり奇妙です。スクリーンショットをご覧ください。
1 つの画像が間違った場所 (チャネル 227) に配置されています。また、Tex ファイルには "7b" がないことは確かです。すべての画像は同じサイズです。それで、何が問題なのでしょうか?
どのようなアイデアでも歓迎します。
答え1
問題を再現する最小限の例を次に示します。
\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}[31]{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}
\begin{subfigure}[227]{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-b}
\caption{Channel 227}\label{fig:227}
\end{subfigure}
\end{figure}
\end{document}
エラーは間違った構文が使用されている\begin{subfigure}
ことにあります。オプションの引数は単一の文字 b
、c
または t
。
修正: 間違った部分を削除する
\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-b}
\caption{Channel 227}\label{fig:227}
\end{subfigure}
\end{figure}
\end{document}
何が起こるのですか?
のコードは\begin{subfigure}
オプションの引数が指定されているかどうかを確認し、指定されていない場合は指定されていると仮定しますb
。その後、次のようなコードを実行します。
\if<optional argument>b%
<code when test returns true>%
\else
<code when test returns false>%
\fi
この227
場合、テキストは
\if227b
<code when test returns true>%
\else
<code when test returns false>%
\fi
そしてテストが返ってくる真実\if
は、次の 2 つの展開不可能なトークン (この場合は2
と )を比較するためです2
。規則により、 は<true code>
テストの直後に始まり、\else
(または\fi
がない場合は\else
) で終了します。そのため、この場合は も含まれます7b
。
スペースなしで行ごとに 5 つの画像を取得するにはどうすればよいでしょうか?
の後の行末によって生成される 2 つのサブ図間のスペースを削除する必要があります\end{subfigure}
。
TeX が制御シーケンスの後のスペースを削除するという事実を利用できます。
\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}
\newcommand{\subfiguresep}{\hspace{0pt}}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{0.2\textwidth}
\includegraphics[width = \textwidth]{example-image-b}
\caption{Channel 227}\label{fig:227}
\end{subfigure}\subfiguresep
\end{figure}
\end{document}
\hspace{0pt}
を使用すると、スペースを追加せずに改行できます。最終行を左揃えにしたい場合は、 の代わりに を使用し\raggedright
ます\centering
。
サブ図の間にスペースを入れたい場合は、次のようにします。
\documentclass{article}
\usepackage{subcaption}
\usepackage{graphicx}
\newcommand{\subfiguresep}{\hspace{2pt}}
\captionsetup[subfigure]{font=footnotesize}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-a}
\caption{Channel 31}\label{fig:31}
\end{subfigure}\subfiguresep
\begin{subfigure}{\dimexpr 0.2\textwidth-8pt}
\includegraphics[width = \textwidth]{example-image-b}
\caption{Channel 227}\label{fig:227}
\end{subfigure}\subfiguresep
\end{figure}
\end{document}
サブキャプションのフォント サイズを小さくしないと、画像の幅を超えてしまうことに注意してください。