感謝之前提出的問題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
如果你想堅持使用 a matrix
,你可以簡化事情。首先,我將使用該選項matrix of nodes
(您需要matrix
為此載入庫),這使得設定矩陣節點的樣式變得更加容易。
然後,您可以使用目前計數器自動設定標籤figure
(並向其添加 1,因為它僅在\caption
排版後更新)並將您透過樣式提供的參數附加到它lablum
。
請注意,已經可以使用特定的節點名稱來引用矩陣節點,例如矩陣左上角的第一個節點將是mat-1-1
,因此您不需要定義自己的節點名稱,但因為您想要以boustropedonic 方式放置子圖形,我按照你的方式放置。
至於您的問題是圖形填滿整個頁面:這可能是由於子圖的大小造成的。嘗試稍微縮放它們。下面的範例顯示,如果可能的話,應將圖形放置在帶有文字的頁面上:
\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}
你可以讓蒂kZ/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}