感谢之前提出的问题如何在 Latex 中用箭头连接图像?
我能够创建一组由箭头连接的图像。但是,我有两个问题:
- 我如何标记每幅图像(在文中参考图 1.2 或 1.5)
- 由于某些原因,生成的图像填满了整个页面(视觉上超过半页),或者至少不允许在图像的同一页上显示任何文本。有没有什么办法可以纠正这个问题?
谢谢
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[lablum/.style={label=below:#1,name=img-#1},
marr/.style={line width=1mm,-latex}]
\matrix[column sep=1cm,row sep=5mm] (mat)
{ \node[lablum=1]{\includegraphics[width=3cm]{example-image- duck}};
& \node[lablum=2]{\includegraphics[width=3cm]{example-image-duck}};\\
\node[lablum=4]{\includegraphics[width=3cm]{example-image-duck}};
& \node[lablum=3]{\includegraphics[width=3cm]{example-image-duck}};\\
\node[lablum=5]{\includegraphics[width=3cm]{example-image-duck}};
& \node[lablum=6]{\includegraphics[width=3cm]{example-image-duck}};\\
};
\draw[marr] (img-1) -- (img-2);
\draw[marr] ([xshift=1mm]img-2.south east) coordinate (aux)
-- (img-3.north-|aux);
\draw[marr] (img-3) -- (img-4);
\draw[marr] ([xshift=-1mm]img-4.south west) coordinate (aux)
-- (img-5.north-|aux);
\draw[marr] (img-5) -- (img-6);
\end{tikzpicture}
\caption{A matrix of figures.}
\label{fig:pffft}
\end{figure}
\end{document}
答案1
如果您想坚持使用matrix
,您可以简化事情。首先,我会使用选项matrix of nodes
(您需要matrix
为此加载库),这使得设置矩阵节点的样式变得更加容易。
然后,您可以使用当前计数器自动设置标签figure
(并向其添加 1,因为它仅在\caption
排版后更新)并将您通过样式提供的参数附加到它lablum
。
请注意,已经可以使用特定的节点名称来引用矩阵节点,例如,矩阵左上角的第一个节点是mat-1-1
,因此您不需要定义自己的节点名称,但由于您想以牛耕式方式放置子图,我保留了您的方式。
至于您遇到的图形填满整个页面的问题:这可能是由于子图的大小造成的。尝试稍微缩放它们。下面的示例显示,如果可能的话,应该将图形放在带有文本的页面上:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{figure}
\centering
\begin{tikzpicture}[
lablum/.style={
label=below:{\pgfmathparse{int(\thefigure+1)}\pgfmathresult.#1},
name={img-#1},
},
marr/.style={
line width=1mm,
-latex
}]
\matrix[matrix of nodes, column sep=1cm, row sep=5mm] (mat)
{ |[lablum=1]| \includegraphics[width=3cm]{example-image-duck} &
|[lablum=2]| \includegraphics[width=3cm]{example-image-duck} \\
|[lablum=4]| \includegraphics[width=3cm]{example-image-duck} &
|[lablum=3]| \includegraphics[width=3cm]{example-image-duck} \\
|[lablum=5]| \includegraphics[width=3cm]{example-image-duck} &
|[lablum=6]| \includegraphics[width=3cm]{example-image-duck} \\
};
\draw[marr] (img-1) -- (img-2);
\draw[marr] ([xshift=1mm]img-2.south east) coordinate (aux)
-- (img-3.north-|aux);
\draw[marr] (img-3) -- (img-4);
\draw[marr] ([xshift=-1mm]img-4.south west) coordinate (aux)
-- (img-5.north-|aux);
\draw[marr] (img-5) -- (img-6);
\end{tikzpicture}
\caption{A matrix of figures.}
\label{fig:pffft}
\end{figure}
\end{document}
你可以让 Ti钾Z/PGF 还可以完全自动为您计算标签(您只需要以某种方式预先告知列数):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[
boustrophedon matrix/.style args={#1/#2}{
matrix of nodes,
nodes in empty cells,
boustrophedon matrix column count/.initial={#2},
boustrophedon matrix step cell counter/.code={
\pgfmathparse{
mod(\pgfmatrixcurrentrow, 2) == 0 ?
int(\pgfkeysvalueof{/tikz/boustrophedon matrix column count} *
\pgfmatrixcurrentrow -
\pgfmatrixcurrentcolumn + 1) :
int(\pgfkeysvalueof{/tikz/boustrophedon matrix column count} *
(\pgfmatrixcurrentrow - 1) +
\pgfmatrixcurrentcolumn)
}
\global\pgfkeyslet{/tikz/boustrophedon matrix cell counter}{\pgfmathresult}
},
execute at begin cell={
|[boustrophedon matrix step cell counter, label={below:{
\pgfmathparse{int(\thefigure+1)}\pgfmathresult.%
\pgfkeysvalueof{/tikz/boustrophedon matrix cell counter}
}
},
name={#1-index-\pgfkeysvalueof{/tikz/boustrophedon matrix cell counter}}]|
},
},
marr/.style={
line width=1mm,
-latex
}]
\matrix[boustrophedon matrix={matrix one/2}, column sep=1cm, row sep=5mm]{
\includegraphics[width=3cm]{example-image-duck} &
\includegraphics[width=3cm]{example-image-duck} \\
\includegraphics[width=3cm]{example-image-duck} &
\includegraphics[width=3cm]{example-image-duck} \\
\includegraphics[width=3cm]{example-image-duck} &
\includegraphics[width=3cm]{example-image-duck} \\
};
\draw[marr] (matrix one-index-1) -- (matrix one-index-2);
\draw[marr] (matrix one-index-2.south east) -- (matrix one-index-3.north east);
\draw[marr] (matrix one-index-3) -- (matrix one-index-4);
\draw[marr] (matrix one-index-4.south west) -- (matrix one-index-5.north west);
\draw[marr] (matrix one-index-5) -- (matrix one-index-6);
\end{tikzpicture}
\caption{A matrix of figures.}
\label{fig:pffft}
\end{figure}
\end{document}
有三列:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[
boustrophedon matrix/.style args={#1/#2}{
matrix of nodes,
nodes in empty cells,
boustrophedon matrix column count/.initial={#2},
boustrophedon matrix step cell counter/.code={
\pgfmathparse{
mod(\pgfmatrixcurrentrow, 2) == 0 ?
int(\pgfkeysvalueof{/tikz/boustrophedon matrix column count} * (\pgfmatrixcurrentrow) -
\pgfmatrixcurrentcolumn + 1) :
int(\pgfkeysvalueof{/tikz/boustrophedon matrix column count} * (\pgfmatrixcurrentrow - 1) +
\pgfmatrixcurrentcolumn)
}
\global\pgfkeyslet{/tikz/boustrophedon matrix cell counter}{\pgfmathresult}
},
execute at begin cell={
|[boustrophedon matrix step cell counter, label={below:{
\pgfmathparse{int(\thefigure+1)}\pgfmathresult.%
\pgfkeysvalueof{/tikz/boustrophedon matrix cell counter}
}
},
name={#1-index-\pgfkeysvalueof{/tikz/boustrophedon matrix cell counter}}]|
},
},
marr/.style={
line width=1mm,
-latex
}]
\matrix[boustrophedon matrix={matrix two/3}, column sep=1cm, row sep=5mm]{
\includegraphics[width=3cm]{example-image-duck} &
\includegraphics[width=3cm]{example-image-duck} &
\includegraphics[width=3cm]{example-image-duck} \\
\includegraphics[width=3cm]{example-image-duck} &
\includegraphics[width=3cm]{example-image-duck} &
\includegraphics[width=3cm]{example-image-duck} \\
};
\draw[marr] (matrix two-index-1) -- (matrix two-index-2);
\draw[marr] (matrix two-index-2) -- (matrix two-index-3);
\draw[marr] (matrix two-index-3.south east) -- (matrix two-index-4.north east);
\draw[marr] (matrix two-index-4) -- (matrix two-index-5);
\draw[marr] (matrix two-index-5) -- (matrix two-index-6);
\end{tikzpicture}
\caption{A matrix of figures.}
\label{fig:pffft}
\end{figure}
\end{document}
使用包进行引用可能更容易subcaption
,但您始终可以创建自己的引用计数器(我创建了一个\refsetcounter
基于的宏\refstepcounter
,但不会增加计数器,而是将其设置为特定值,这在这里很方便):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\newcounter{matrix}
\newcounter{matrixnode}[matrix]
\renewcommand*{\thematrixnode}{\thematrix.\arabic{matrixnode}}
\makeatletter
\newcommand{\refsetcounter}[2]{\setcounter{#1}{#2}%
\protected@edef\@currentlabel
{\csname p@#1\endcsname\csname the#1\endcsname}%
}
\makeatother
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[
boustrophedon matrix/.style args={#1/#2}{
matrix of nodes,
nodes in empty cells,
boustrophedon matrix column count/.initial={#2},
boustrophedon matrix step cell counter/.code={
\pgfmathparse{
mod(\pgfmatrixcurrentrow, 2) == 0 ?
int(\pgfkeysvalueof{/tikz/boustrophedon matrix column count} * (\pgfmatrixcurrentrow) -
\pgfmatrixcurrentcolumn + 1) :
int(\pgfkeysvalueof{/tikz/boustrophedon matrix column count} * (\pgfmatrixcurrentrow - 1) +
\pgfmatrixcurrentcolumn)
}
\global\pgfkeyslet{/tikz/boustrophedon matrix cell counter}{\pgfmathresult}
},
row 1 column 1/.append style={
execute at begin cell={
\refstepcounter{matrix}
}
},
execute at begin cell={
|[boustrophedon matrix step cell counter, label={below:{
\pgfmathparse{int(\thefigure+1)}\pgfmathresult.%
\pgfkeysvalueof{/tikz/boustrophedon matrix cell counter}
\refsetcounter{matrixnode}{\pgfkeysvalueof{/tikz/boustrophedon matrix cell counter}}
\label{matrixnode:#1-\pgfkeysvalueof{/tikz/boustrophedon matrix cell counter}}
}
},
name={#1-index-\pgfkeysvalueof{/tikz/boustrophedon matrix cell counter}}]|
},
},
marr/.style={
line width=1mm,
-latex
}]
\matrix[boustrophedon matrix={matrix two/3}, column sep=1cm, row sep=5mm]{
\includegraphics[width=3cm]{example-image-duck} &
\includegraphics[width=3cm]{example-image-duck} &
\includegraphics[width=3cm]{example-image-duck} \\
\includegraphics[width=3cm]{example-image-duck} &
\includegraphics[width=3cm]{example-image-duck} &
\includegraphics[width=3cm]{example-image-duck} \\
};
\draw[marr] (matrix two-index-1) -- (matrix two-index-2);
\draw[marr] (matrix two-index-2) -- (matrix two-index-3);
\draw[marr] (matrix two-index-3.south east) -- (matrix two-index-4.north east);
\draw[marr] (matrix two-index-4) -- (matrix two-index-5);
\draw[marr] (matrix two-index-5) -- (matrix two-index-6);
\end{tikzpicture}
\caption{A matrix of figures.}
\label{fig:pffft}
\end{figure}
See subfigure \ref{matrixnode:matrix two-1} and \ref{matrixnode:matrix two-4}.
\end{document}
实际上,我不太清楚你想要什么。如果你想让标签和标题保持可定制,也许可以这样做:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\newcounter{matrix}
\newcounter{matrixnode}[matrix]
\renewcommand*{\thematrixnode}{\thematrix.\arabic{matrixnode}}
\makeatletter
\newcommand{\refsetcounter}[2]{\setcounter{#1}{#2}%
\protected@edef\@currentlabel
{\csname p@#1\endcsname\csname the#1\endcsname}%
}
\makeatother
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[
my custom matrix/.style={
matrix of nodes,
nodes in empty cells,
nodes={
fill=red!10,
minimum height=3.25cm,
minimum width=3.25cm,
},
every label/.style={
minimum height=0pt,
},
lablum/.style args={##1/##2}{
label=below:{%
\pgfmathparse{int(\thefigure+1)}\pgfmathresult.##1: ##2%
\refsetcounter{matrixnode}{##1}\label{matrixnode:#1-##1}%
},
name={#1-index-##1},
},
my custom matrix initialise/.code={
\refstepcounter{matrix}
},
my custom matrix initialise
},
marr/.style={
line width=1mm,
-latex
}]
\matrix[column sep=1cm, row sep=5mm, my custom matrix={mat}] {
|[lablum={1/my caption}]| \includegraphics[width=3cm]{example-image-duck} &
|[lablum={2/foo}]| \includegraphics[width=3cm]{example-image-duck} \\
|[lablum={7/bar}]| \includegraphics[width=3cm]{example-image-duck} &
|[lablum={5/baz}]| \includegraphics[width=3cm]{example-image-duck} &
|[lablum={3/foo}]| \includegraphics[width=3cm]{example-image-duck} \\
};
\draw[marr] (mat-index-1) -- (mat-index-2);
\draw[marr] (mat-index-3) -- (mat-index-5);
\end{tikzpicture}
\caption{A matrix of figures.}
\label{fig:pffft}
\end{figure}
See subfigure \ref{matrixnode:mat-1} and \ref{matrixnode:mat-5}.
\end{document}
我添加了一些样式,使矩阵单元格具有相同的大小。为了使这一点更清晰,我为要显示的节点添加了一些颜色,您可以简单地删除它们(通过删除fill=red!10
)。
最后,使用该包可能会让事情变得容易得多subcaption
,但是,您将无法轻松地自由定位节点(例如从右到左):
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{subcaption}
\captionsetup[subfigure]{labelformat=simple}
\DeclareCaptionSubType*{figure}
\renewcommand\thesubfigure{\thefigure.\arabic{subfigure}}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[
marr/.style={
line width=1mm,
-latex
}]
\matrix[matrix of nodes, column sep=1cm, row sep=5mm] (mat) {
\subcaptionbox{foo\label{subfig:1-1}}{\includegraphics[width=3cm]{example-image-duck}} &
\subcaptionbox{bar\label{subfig:1-2}}{\includegraphics[width=3cm]{example-image-duck}} \\
\subcaptionbox{baz\label{subfig:1-3}}{\includegraphics[width=3cm]{example-image-duck}} &
\subcaptionbox{foo\label{subfig:1-4}}{\includegraphics[width=3cm]{example-image-duck}} &
\subcaptionbox{bar\label{subfig:1-5}}{\includegraphics[width=3cm]{example-image-duck}} \\
};
\draw[marr] (mat-1-1) -- (mat-1-2);
\end{tikzpicture}
\caption{A matrix of figures.}
\label{fig:pffft}
\end{figure}
See subfigure \ref{subfig:1-1} and \ref{subfig:1-5}.
\end{document}