
TikZ 画像を本文に対してどのように配置すればよいですか? TikZ を使用して円を描き、それを本文の横に配置したいのですが (読むときに見やすく、邪魔にならないようにするため)、どうすればよいかわかりません。座標を変更して画像を移動しようとしましたが、全体を移動するのではなく、図同士の相対移動のみのようです。この点について、どなたか助けていただければ幸いです。ありがとうございます!
\documentclass{minimal}
\usepackage{tikz}
\begin{document}
Body text.
\begin{tikzpicture}
\draw [fill = lightgray] (0,0) circle [radius = 1.25 cm];
\end{tikzpicture}
\caption{Circular Region}
\end{document}
答え1
次の例を明確にするために、内部にテキストが入った円などの単純な tikz 画像を生成するマクロを定義します。
\newcommand{\mypicture}[1][]{% 1 optional parameter for options for the tikz picture
\begin{tikzpicture}[#1]
\node[draw, circle, fill=yellow!30, inner sep=2mm] (a) {A};
\end{tikzpicture}
}
これによって次のものが生成されます:
TikZ の図が完成すると、TeX では単なるボックスになります。次のように、段落の一部になる「大きな文字」と考えてください。
This paragraph contains a figure \mypicture{} inline.
ご覧のとおり、画像を含む「ボックス」は、ベースラインでテキストの残りの部分と揃えられています。各文字には、同じ行の他の文字と垂直に揃えるために使用される仮想線があります。ボックスはベースラインの上下に拡張できますが、デフォルトでは、tikz は画像のベースラインを結果のボックスの一番下に配置します。
baseline
これを変更して、 tikzpicture にオプションを指定できます。このオプションは、引数として、 などの寸法、または、1mm
などの図内の任意の座標の名前、さらにはノード内のテキストのベースラインである を受け取ることができます。これらのケースを見てみましょう。a.center
a.north
a.base
a
\parbox{6cm}{
\begin{itemize}
\item Default: \hrulefill\mypicture
\item 2mm: \hrulefill\mypicture[baseline=-2mm]
\item Center: \hrulefill\mypicture[baseline=(a.center)]
\item North: \hrulefill\mypicture[baseline=(a.north)]
\item Base: \hrulefill\mypicture[baseline=(a.base)]
\end{itemize}
}
もちろん、これを「インライン」で使用するだけでなく、脚注、欄外注、表、図の環境 (フロートする) など、他のテキストを配置できる場所ならどこにでも配置できます。欄外注の例を見てみましょう。
\marginpar{\mypicture[baseline=(a.base)]}
\lipsum[1]
\lipsum[2]
wrapfig
図や表の周囲にテキストを折り返すことを可能にするなどのパッケージと組み合わせて使用することもできます。
tikz オプションremember picture
とを使用するとoverlay
、他の回答に示すように、ページ内の任意の絶対位置に配置できます。
答え2
remember picture,overlay
オプションとノードを使用できますcurrent page
。次の例では、tikznodepages
ノードを使用してさらに細かい制御を行うには、パッケージを使用しますcurrent page text area
。
\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{tikz}
\usepackage{tikzpagenodes}
\usepackage{lipsum}
\begin{document}
\begin{tikzpicture}[remember picture,overlay]
\draw[fill = lightgray] ([xshift=0.7cm]current page text area.east) circle [radius = 0.5 cm];
\draw[fill = red!40] ([xshift=-0.7cm]current page text area.west) circle [radius = 0.5 cm];
\draw[fill = green!40] ([yshift=0.7cm]current page text area.north) circle [radius = 0.5 cm];
\draw[fill = blue!40] ([yshift=-0.7cm]current page text area.south) circle [radius = 0.5 cm];
\draw[fill = magenta!40] ([xshift=0.7cm,yshift=0.7cm]current page.south west) circle [radius = 0.5 cm];
\draw[fill = olive!40] ([xshift=-0.7cm,yshift=-0.7cm]current page.north east) circle [radius = 0.5 cm];
\end{tikzpicture}
\lipsum[4]\lipsum[4]\lipsum[4]
\end{document}