我想制作下图:
我收到了类似图片的代码带有子图和标题的 Tikzpicture但无法将其更改为如上所示的所需格式。
\documentclass{article}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{tikz}
\begin{document}
\begin{figure}
\centering
\subcaptionbox{subfigure a}[\textwidth]{
\begin{tikzpicture}
\def\length{6} % Length of your arrow
\def\height{0.5} % Height of your intersections
\draw[->] (-\length,0) -- (\length,0);
\node at (-2*\length/3,0) [yshift=-4,anchor=north]{X};
\node at ( 0,0) [yshift=-4,anchor=north]{Y};
\node at ( 2*\length/3,0) [yshift=-4,anchor=north]{Z};
\draw[-] (-\length/3,-\height/2) -- (-\length/3,\height/2);
\draw[-] ( \length/3,-\height/2) -- ( \length/3,\height/2);
\end{tikzpicture}
}
\subcaptionbox{subfigure b}[\textwidth]{
\begin{tikzpicture}
\def\length{6} % Length of your arrow
\def\height{0.5} % Height of your intersections
\draw[->] (-\length,0) -- (\length,0);
\node at (-2*\length/3,0) [yshift=-4,anchor=north]{X};
\node at ( 0,0) [yshift=-4,anchor=north]{Y};
\node at ( 2*\length/3,0) [yshift=-4,anchor=north]{Z};
\draw[-] (-\length/3,-\height/2) -- (-\length/3,\height/2);
\draw[-] ( \length/3,-\height/2) -- ( \length/3,\height/2);
\end{tikzpicture}
}
\end{figure}
\end{document}
答案1
最好使用,subcaption block environment
这将允许您改变vertical
我使用的图形之间的分隔\captionsetup{skip=2em}
,然后使用这个环境您可以控制标题是否出现below
或above
,这是定义如果您将标题放在绘图之前或之后;对于您提出的绘图,我已经创建了 3 个绘图定义,以便不必多次指定样式和坐标。第一个\Xdoms
,允许您将标记放置在相对于和之间的因子的位置0
,1
该因子为指示长度,在本例中为文本行的宽度\linewidht
,这样您就可以更好地放置标记,每个标记必须有一个name
、一个content
和一个color
,names
将用于其他绘图定义,以免放置绝对坐标。
第二个\Acot
,在使用它们的定义的两个标记之间绘制尺寸names
,请记住,在 中\xdoms
有2
命名坐标,即name
和name-up
,这是尺寸的开始位置,然后内容进入middle
(pos=0.5
)和above
路径中的文本节点。
最后一个用于放置中点“x”,它使用calc
带有方法的tikz 库$(#1)!0.5!(#2)$
,定位两个可以作为名称输入的节点之间的中点,然后绘制一个圆圈并定义一个文本节点,但我没有放置相应的文本,而是使用可以将其放置在距离节点node label
一定距离的特定方向上。label distance=0.7em
-90
通过这样做,可以绘制所有情况,包括作为游乐场的附加情况,显然您必须首先使用\xdoms
,然后再使用其余的,因为它们使用在输入的每个标记中创建的坐标的名称。
结果:
梅威瑟:
\documentclass{article}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,calc}
\def\Xdoms#1#2#3{%\Xdoms{length}{height}{Markers{position/name/content/color}}
\draw(0,0)--++(#1,0);
\foreach \position/\name/\content/\color in {#3}{
\draw[draw=\color,thick]
(#1*\position,0)++(0,#2/2)
-- ++(0,-#2) node[below, minimum height=2em,inner sep=0]{\content};
\coordinate (\name-up) at (#1*\position,#2*1.5);
\coordinate (\name) at (#1*\position,0);
}
}
\def\Acot#1#2#3#4{
\draw[draw=#4,|-|](#1)--(#2)node [pos=0.5,above]{#3};
}
\def\Dotx#1#2#3#4{
\fill[fill=#4]($(#1)!0.5!(#2)$) circle (3.5pt) node[below,label={[label distance=0.7em,#4]-90:#3}]{};
}
\begin{document}
\setcounter{figure}{4}
\begin{figure}
\centering
\caption {figure name}
\begin{subcaptionblock}{\textwidth}
\begin{tikzpicture}
\Xdoms{\linewidth}{0.5}{
0/Mk-0/0/black,%\position/\name/\content/\color
0.45/Mk-a/a/black,
0.65/Mk-b/b/black,
1/Mk-1/1/black%
}
\Acot{Mk-0-up}{Mk-a-up}{Text\_1}{red}
\Acot{Mk-b-up}{Mk-1-up}{Text\_2}{blue}
\Dotx{Mk-b}{Mk-1}{x}{black}
\end{tikzpicture}
\captionsetup{skip=2em}
\caption{Sub figure a}
\end{subcaptionblock}
\begin{subcaptionblock}{\textwidth}
\begin{tikzpicture}
\Xdoms{\linewidth}{0.5}{
0/Mk-0/0/black,%\position/\name/\content/\color
0.5/Mk-a/a/black,
1/Mk-1/1/black%
}
\Acot{Mk-0-up}{Mk-a-up}{Text\_1}{red}
\Acot{Mk-a-up}{Mk-1-up}{Text\_2}{blue}
\Dotx{Mk-a}{Mk-1}{x}{black}
\end{tikzpicture}
\captionsetup{skip=2em}
\caption{Sub figure b}
\end{subcaptionblock}
\begin{subcaptionblock}{\textwidth}
\begin{tikzpicture}
\Xdoms{\linewidth}{0.5}{
0/Mk-0/0/black,%\position/\name/\content/\color
1/Mk-1/1/black%
}
\Acot{Mk-0-up}{Mk-1-up}{Text\_2}{blue}
\Dotx{Mk-0}{Mk-1}{x}{black}
\end{tikzpicture}
\captionsetup{skip=2em}
\caption{Sub figure c}
\end{subcaptionblock}
\begin{subcaptionblock}{\textwidth}
\begin{tikzpicture}
\Xdoms{\linewidth}{0.5}{
0/AA/A/red,%\position/\name/\content/\color
0.1/BB/B/black!20!red,
0.3/CC/C/black!30!red,
0.4/DD/D/black!40!red,
0.5/EE/E/black!50!red,
0.75/FF/F/black!60!red,
1/GG/G/black!70!red%
}
\Acot{AA-up}{CC-up}{A TO C}{blue}
\Acot{DD-up}{FF-up}{D TO F}{blue!50!red}
\Dotx{AA}{BB}{x}{green!50!blue}
\Dotx{EE}{FF}{y}{green!50!cyan}
\end{tikzpicture}
\captionsetup{skip=2em}
\caption{Sub figure d playground}
\end{subcaptionblock}
\end{figure}
\end{document}