図の挿入 メモ †はじめに †Web検索で見つかる様々な \(\LaTeX\) に関するページでは,
ベクター画像の例として .eps を使っているページが多いが,これは一昔前の時代の名残らしい. 図の挿入の基本 †単純に1枚だけ挿入する時は, \begin{figure}[tb]
\centering
\includegraphics[keepaspectratio,width=\linewidth]{filename.pdf}
\caption{An example.}
\label{fig:hogehoge}
\end{figure}
以下にそれぞれの行で実行しているものについて説明する. \begin{figure} †\begin{figure}[htbp]
\end{figure}
のhtbpは,here, top, bottom, page を表し,挿入する位置を指定する. \begin{figure}[!h]
のように ! をつけることで若干無理を通してでも h になる. \centering †普通は \begin{figure} で確保した領域の上端左寄せで図が入るので,図を中央寄せで表示する. \includegraphics{filename.pdf} †オプションの keepaspectratio は名の通りアスペクト比固定. [keepaspectratio, width=\linewidth] とすることで,縦横比そのままで確保した幅いっぱいに図が入る. \caption{foobar} †表示されるキャプション.位置の調整をしたい場合は \hspace,\vspace を駆使する. \label{fig:hogehoge} †\label{fig:hoge}
と記述すれば,本文中で \ref{fig:hoge}
と書くことにより図表番号が自動的に反映される.本文中での表示フォーマットは cls ファイル次第. 2段構成のページにぶち抜きで挿入 †論文の書式が2段構成で,図だけ1段(margin を除いたページ幅いっぱい)で使う場合は,\begin{figure*} のように,*をつける. \begin{figure*}[!t]
\centering
\includegraphics[keepaspectratio, width=\linewidth]{filename.pdf}
\caption{An example.}
\label{fig:hogehoge}
\end{figure*}
これを実行すると,この図が最後のページに現れやすいので \begin{figure*}[!t]
の!tで強めに入れた方が整う. 同じ枠内に複数枚 †figure の中で minipage を使って,画像一つ分の領域を確保してから includegraphics を使う. \usepackage[subrefformat=parens]{subcaption}
subcaption パッケージが使用しているスタイルと競合する場合は諦めて,
(a)や(b)などの文字を直接打ち込んで挿入する. % --- Figure ------------------------------
\begin{figure*}[!t]
% -- (a) --
\begin{minipage}{0.5\linewidth}
\centering
\includegraphics[keepaspectratio, width=0.95\linewidth]{fig/a.pdf} % 図の大きさに合わせてscaleやwidth=\linewidthで調整
\subcaption{1つめ}
\end{minipage}
% ---------
% \vspace と \hspace を駆使して間隔を調節
% -- (b) --
\begin{minipage}{0.5\linewidth}
\centering
\includegraphics[keepaspectratio, width=0.95\linewidth]{fig/b.pdf}
\subcaption{2つめ}
\end{minipage}
% ---------
%% main caption
\caption{Hogehoge}
\label{fig:1}
\end{figure*}
% -----------------------------------------
\minipage を応用すれば任意の配置で図を置くことができる. % --- Figure ------------------------------
\begin{figure}[t]
% ---------------------------------
\begin{tabular}{c} % {c}の数は表現したいレイアウトに応じて調整
% -- (a) --
\begin{minipage}{0.5\linewidth}
\centering
\includegraphics[keepaspectratio, width=\linewidth]{fig/a.pdf}
\subcaption{1つめ}
\end{minipage}
% ---------
% -- (b) --
\begin{minipage}{0.5\linewidth}
\centering
\includegraphics[keepaspectratio, width=\linewidth]{fig/b.pdf}
\subcaption{2つめ}
\end{minipage}
% ---------
\\ % ここで次の行へ (tabularの効果)
% -- (c) --
\begin{minipage}{0.5\linewidth}
\centering
\includegraphics[keepaspectratio, width=\linewidth]{fig/c.pdf}
\subcaption{3つめ}
\end{minipage}
% ---------
% -- (d) --
\begin{minipage}{0.5\linewidth}
\centering
\includegraphics[keepaspectratio, width=\linewidth]{fig/d.pdf}
\subcaption{4つめ}
\end{minipage}
% ---------
\end{tabular}
% ---------------------------------
\caption{全体のキャプション}
\label{fig:2x2のレイアウト例}
\end{figure}
% -----------------------------------------
begin{tabular}{ccc} などと複数セルを指定した場合には,同一行内のセル区切りで & をいれることを忘れないように. 図中(図上)にa)やb)などのテキストを挿入 †いくつかの subpanel で構成された図で a) や b) など番号を振りたい時は,図の上に別途テキストを載せた方が微調整時にも図を描き直さなくてよいので便利. \usepackage{overpic}
をプリアンブルで書いた上で, \includegraphics[keepaspectratio,width=\linewidth]{subfigA.pdf}
の箇所を \begin{overpic}[keepaspectratio,width=\linewidth]{subfigA.pdf}
\put(0,80){\Large{\textsf{a)} } }
\end{overpic}
のようにするだけ.(0,80)は横方向,縦方向の相対的な位置.(0,0)が左下原点で(100,100)が右上端.文字の大きさやフォントは都合に応じて変更する. % ------------------------------------------------
\begin{figure}[p]
%% -- a) ---
\begin{minipage}{\linewidth}
\centering
%\includegraphics[keepaspectratio,width=\linewidth]{subfigA.pdf} 通常はこっち
\begin{overpic}[keepaspectratio,width=\linewidth]{subfigA.pdf}
\put(0,80){\Large{\textsf{a)} } }
\end{overpic}
\end{minipage}
%% -- b) ---
\begin{minipage}{\linewidth}
\centering
\begin{overpic}[keepaspectratio,width=\linewidth]{subfigB.pdf}
\put(0,80){\Large{\textsf{b)} } }
\end{overpic}
\end{minipage}
\caption{caption text}
\label{fig:label}
\end{figure}
% ------------------------------------------------
右/左寄せで行内に埋め込み †論文では使わないが,某申請書やレポート形式の文書では,ページの上端下端以外に図を挿入することもある. \usepackage{wrapfig}
をプリアンブルに書いた上で, % --- Figure ------------------------------
\begin{wrapfigure}[11]{r}{70mm} % [埋め込む行数]{左(l)/右(r)寄せ}{埋め込む幅}
\centering
\vspace{-3mm}
\includegraphics[width=70mm,clip]{something.png}
\vspace{-3mm}
\caption{なにかの図} \label{fig:something}
\end{wrapfigure}
% -----------------------------------------
とか. キャプションの字下げ †キャプションのインデント(字下げ)をするにはプリアンブルで次のように書く. \usepackage{caption}
\captionsetup{format=hang}
ただし,sty ファイルの中で図表の書式が定まっているときは,書式が変わってしまう可能性がある. TikZ の図を挿入 †TikZ のコードで描いた図をそのまま埋め込みたいとき. \begin{tikzpicture}
で始まり \end{tikzpicture}
で終わるコードを書いたとして,この図を文書に挿入するには次のようにする. \begin{figure}[tb]
\centering
\resizebox{\linewidth}{!}{\input{hogehoge.tex} }
\caption{TikZの図を埋め込む.}
\end{figure}
そのままinputしようとすると大抵サイズが合わないので, \resizebox{width}{height}{content}
で調整する.上記の例のように,resizebox の引数の width, heightのどちらかを指定してもう一方を{!}にすれば,縦横比が固定される. \usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{%
decorations.pathreplacing,%
decorations.pathmorphing%
}
|