我已经创建了一个框图,并想向其中添加一个图例,该图例应位于最左上角。
我已经根据以下答案为图例设置了一个自定义环境这问题。没有图例,这是我的结果:
然而,当添加图例时,current bounding box.north west
整个事物由于某种原因发生了变化:
这是代码:
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
% setup the custom legend stuff
% argument #1: any options
\makeatletter
\newenvironment{customlegend}[1][]{%
\begingroup
% inits/clears the lists (which might be populated from previous
% axes):
\pgfplots@init@cleared@structures
\pgfplotsset{#1}%
}{%
% draws the legend:
\pgfplots@createlegend
\endgroup
}%
% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\pgfplots@addlegendimage}
\makeatother
\begin{document}
\begin{tikzpicture}
[
node distance=50pt
]
\newcommand{\nodesize}{12pt}
\linespread{0.8}
\node (S0) [circle,draw,inner sep=\nodesize,very thick]
{$$\mbox{\Large $0$}$$};
\node (S1) [circle,draw,inner sep=\nodesize, right =of S0 ,very thick]
{$$\mbox{\Large $1$}$$};
\node(S2A) [circle,draw,inner sep=\nodesize, above right =of S1 ,very thick]
{$$\mbox{\Large $2^a$}$$};
\node(S2B) [circle,draw,inner sep=\nodesize, below right =of S1 ,very thick]
{$$\mbox{\Large $2^b$}$$};
\node (S3) [circle,draw,inner sep=\nodesize, below right =of S2A,very thick]
{$$\mbox{\Large $3$}$$};
\node (S4) [circle,draw,inner sep=\nodesize, right =of S3,very thick]
{$$\mbox{\Large $4$}$$};
\draw[->, gray] (S0.10) -- (S1.170);
\draw[->, gray] (S1.190) -- (S0.350);
\draw[->, gray] (S1.90) -- (S2A.180);
\draw[->, gray] (S1.290) -- (S2B.160);
\draw[->, gray] (S2B.180) -- (S1.270);
\draw[->, gray] (S2A.260) -- (S2B.100);
\draw[->, gray] (S2B.80) -- (S2A.280);
\draw[->, gray] (S2A.0) -- (S3.90);
\draw[->, gray] (S2B.20) -- (S3.250);
\draw[->, gray] (S3.270) -- (S2B.0);
\draw[->, gray] (S4.190) -- (S3.350);
\draw[->, gray] (S0) to [in=80,out=30,looseness=4.5] (S0);
\draw[->, gray] (S1) to [in=100,out=150,looseness=4.5] (S1);
\draw[->, gray] (S2B) to [in=350,out=300,looseness=4.5] (S2B);
\draw[->, gray] (S3) to [in=80,out=30,looseness=4.5] (S3);
\draw[->, gray] (S1) to [in=280,out=260,looseness=2.3] (S3);
\draw[->, gray] (S3) to [in=240,out=300,looseness=2.7] (S1);
\draw[->] (S3.10) edge node[sloped, anchor=center, above,align=center]
{$t_m$ \\ $$\mbox{\tiny $\mathtt{[0,0,-,1]}$}$$} (S4.170);
\draw[->] (S4) to [in=80,out=30,looseness=4.5] node [above,xshift=-7pt,yshift=2pt,align=center] {$t_{m+1}...t_{n-1}$ \\ $$\mbox{\tiny $\mathtt{[0,0,-,-]}$}$$} (S4);
\draw[->, dashed]
(S2A.200) edge node[sloped, anchor=center, below,align=center] {$t_n$ \\ $$\mbox{\tiny $\mathtt{[1,0,-,-]}$}$$} (S1.70)
(S3.110) edge node[sloped, anchor=center, below,align=center] {$t_m$ \\ $$\mbox{\tiny $\mathtt{[0,0,-,-]}$}$$} (S2A.340)
(S2A) to [in=80,out=30,looseness=4.5] node [right,xshift=2pt,align=center] {$t_{m+1}...t_{n-1}$ \\ $$\mbox{\tiny $\mathtt{[0,0,-,-]}$}$$} (S2A);
% the next few lines add the legend:
\begin{customlegend}[legend style={at={(current bounding box.north west)}}, anchor=north west, legend entries={\textbf{Transitions}, valid, invalid, corrected}]
\addlegendimage{empty legend}
\addlegendimage{black}
\addlegendimage{dotted}
\addlegendimage{dashed}
\end{customlegend}
\end{tikzpicture}
\end{document}
为什么添加自定义图例会改变边界框?我该如何将图例定位在第一张图片的左上角?我知道我可以摆弄一下,legend style={at={x,y)}
但我觉得应该有一个更优雅的解决方案,将图例与我的图表对齐。
答案1
问题似乎是你正在设置anchor
图例的外部,这legend style
意味着它实际上并不适用于图例。将其移到 内legend style
,它就会按预期工作。
不相关:您可以避免一些代码重复,例如通过使用scope
如下所示的环境。
我还可以问一下你为什么要使用构造$$\mbox{$..$}$$
?首先,$$ ... $$
根本不应该在 LaTeX 中使用(为什么 \[ ... \] 比 $$ ... $$ 更可取?),然后使用 切换回文本模式\mbox
,然后使用 再次切换回内联数学模式。为什么在这种情况下$.. $
不使用 just ?$ .. $
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
% setup the custom legend stuff
% argument #1: any options
\makeatletter
\newenvironment{customlegend}[1][]{%
\begingroup
% inits/clears the lists (which might be populated from previous
% axes):
\pgfplots@init@cleared@structures
\pgfplotsset{#1}%
}{%
% draws the legend:
\pgfplots@createlegend
\endgroup
}%
% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\pgfplots@addlegendimage}
\makeatother
\begin{document}
\begin{tikzpicture}
[
node distance=50pt
]
\newcommand{\nodesize}{12pt}
\linespread{0.8}
\begin{scope}[every node/.style={circle,draw,very thick,font=\Large,inner sep=\nodesize}]
\node (S0) {$0$};
\node (S1) [right=of S0] {$1$};
\node(S2A) [above right =of S1] {$2^a$};
\node(S2B) [below right =of S1] {$2^b$};
\node (S3) [below right =of S2A] {$3$};
\node (S4) [right =of S3] {$4$};
\end{scope}
\draw[->, gray] (S0.10) -- (S1.170);
\draw[->, gray] (S1.190) -- (S0.350);
\draw[->, gray] (S1.90) -- (S2A.180);
\draw[->, gray] (S1.290) -- (S2B.160);
\draw[->, gray] (S2B.180) -- (S1.270);
\draw[->, gray] (S2A.260) -- (S2B.100);
\draw[->, gray] (S2B.80) -- (S2A.280);
\draw[->, gray] (S2A.0) -- (S3.90);
\draw[->, gray] (S2B.20) -- (S3.250);
\draw[->, gray] (S3.270) -- (S2B.0);
\draw[->, gray] (S4.190) -- (S3.350);
\draw[->, gray] (S0) to [in=80,out=30,looseness=4.5] (S0);
\draw[->, gray] (S1) to [in=100,out=150,looseness=4.5] (S1);
\draw[->, gray] (S2B) to [in=350,out=300,looseness=4.5] (S2B);
\draw[->, gray] (S3) to [in=80,out=30,looseness=4.5] (S3);
\draw[->, gray] (S1) to [in=280,out=260,looseness=2.3] (S3);
\draw[->, gray] (S3) to [in=240,out=300,looseness=2.7] (S1);
\draw[->] (S3.10) edge node[sloped, anchor=center, above,align=center]
{$t_m$ \\ \tiny $\mathtt{[0,0,-,1]}$} (S4.170);
\draw[->] (S4) to [in=80,out=30,looseness=4.5] node [above,xshift=-7pt,yshift=2pt,align=center] {$t_{m+1}...t_{n-1}$ \\ \tiny $\mathtt{[0,0,-,-]}$} (S4);
\draw[->, dashed]
(S2A.200) edge node[sloped, anchor=center, below,align=center] {$t_n$ \\ \tiny $\mathtt{[1,0,-,-]}$} (S1.70)
(S3.110) edge node[sloped, anchor=center, below,align=center] {$t_m$ \\ \tiny $\mathtt{[0,0,-,-]}$} (S2A.340)
(S2A) to [in=80,out=30,looseness=4.5] node [right,xshift=2pt,align=center] {$t_{m+1}...t_{n-1}$ \\ \tiny $\mathtt{[0,0,-,-]}$} (S2A);
% the next few lines add the legend:
\begin{customlegend}[
legend style={
at={(current bounding box.north west)},
anchor=north west % <-- inside the legend style
},
legend entries={\textbf{Transitions}, valid, invalid, corrected}
]
\addlegendimage{empty legend}
\addlegendimage{black}
\addlegendimage{dotted}
\addlegendimage{dashed}
\end{customlegend}
\end{tikzpicture}
\end{document}
答案2
的锚点customlegend
应为以下部分legend style
:
\begin{customlegend}[legend style={at={(current bounding box.north west)}, anchor=north west},
legend entries={\textbf{Transitions}, valid, invalid, corrected}]
无关:
通过使用 TikZ 库quotes
,定义节点edge
和edge quotes
样式,您的 MWE 代码会变得更短:
\documentclass[tikz, margin=3mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{positioning,
quotes}
% setup the custom legend stuff
% argument #1: any options
\makeatletter
\newenvironment{customlegend}[1][]{%
\begingroup
% inits/clears the lists (which might be populated from previous
% axes):
\pgfplots@init@cleared@structures
\pgfplotsset{#1}%
}{%
% draws the legend:
\pgfplots@createlegend
\endgroup
}%
% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\pgfplots@addlegendimage}
\makeatother
\begin{document}
\begin{tikzpicture}[auto,
node distance = 50pt,
state/.style = {circle, draw, very thick, inner sep=\nodesize,
font=\Large},
every edge/.style = {draw=gray, semithick, ->},
every edge quotes/.style = {font=\small\linespread{0.6}\selectfont, align=center, sloped}
]
\newcommand{\nodesize}{12pt}
%
\begin{scope}[nodes={state}]
\node (S0) {$0$};
\node (S1) [right=of S0] {$1$};
\node (S2A) [above right=of S1] {$2^a$};
\node (S2B) [below right=of S1] {$2^b$};
\node (S3) [below right=of S2A] {$3$};
\node (S4) [right=of S3] {$4$};
\end{scope}
%
\draw (S0.10) edge (S1.170)
(S1.190) edge (S0.350)
(S1.90) edge (S2A.180)
(S1.290) edge (S2B.160)
(S2B.180) edge (S1.270)
(S2A.260) edge (S2B.100)
(S2B.80) edge (S2A.280)
(S2A.0) edge (S3.90)
(S2B.20) edge (S3.250)
(S3.270) edge (S2B.0)
(S4.190) edge (S3.350)
(S0) edge[in=80, out=30, looseness=4.5] (S0)
(S1) edge[in=100,out=150,looseness=4.5] (S1)
(S2B) edge[in=350,out=300,looseness=4.5] (S2B)
(S3) edge[in=80, out=30, looseness=4.5] (S3)
(S1) edge[in=280,out=260,looseness=2.3] (S3)
(S3) edge[in=240,out=300,looseness=2.7] (S1);
\draw[dashed]
(S3.10) edge["$t_m$ \\ \tiny {$[0,0,-,1]$}"] (S4.170)
(S4) edge[in=80,out=30,looseness=4.5,
"$t_{m+1}\dots t_{n-1}$ \\ \tiny {$[0,0,-,-]$}"] (S4)
(S2A.200) edge["$t_n$ \\ \tiny {$[1,0,-,-]$}" '] (S1.70)
(S3.110) edge["$t_m$ \\ \tiny {$[0,0,-,-]$}" '] (S2A.340)
(S2A) edge[in=80,out=30,looseness=4.5,
"$t_{m+1}\dots t_{n-1}$ \\ \tiny {$[0,0,-,-]$}"] (S2A);
% the next few lines add the legend:
\begin{customlegend}[legend style={at={(current bounding box.north west)}, anchor=north west},
legend entries={\textbf{Transitions}, valid, invalid, corrected}]
\addlegendimage{empty legend}
\addlegendimage{black}
\addlegendimage{dotted}
\addlegendimage{dashed}
\end{customlegend}
\end{tikzpicture}
\end{document}