![如何使用 LaTeX 创建图表来比较天际线操作结果?](https://linux22.com/image/242117/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20LaTeX%20%E5%88%9B%E5%BB%BA%E5%9B%BE%E8%A1%A8%E6%9D%A5%E6%AF%94%E8%BE%83%E5%A4%A9%E9%99%85%E7%BA%BF%E6%93%8D%E4%BD%9C%E7%BB%93%E6%9E%9C%EF%BC%9F.png)
我正在尝试绘制这个,但除了此图中的直线外,我无法正确放置其他东西,例如正方形和圆形。我该怎么做?非常感谢您的帮助。
编辑:由于 MWE 被要求,我将在这里发布我的代码,并在接近上面的图片时对其进行更新。
\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[y=.2cm, x=.7cm,font=\sffamily]
\draw (0,0) -- coordinate (x axis mid) (10,0);
\draw (0,0) -- coordinate (y axis mid) (0,30);
\end{tikzpicture}
\end{document}
答案1
您可以使用以下代码。使用节点创建圆圈,然后我们绘制到节点和从节点开始的图形,因为这样线条就不会穿过它。对于矩形中的圆圈,我们定义了一个新形状,因此我们也可以使用节点来绘制它。代码如下所示:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\makeatletter
\pgfdeclareshape{rectCircle}{
\inheritsavedanchors[from=rectangle] % this is nearly a rectangle
\inheritanchorborder[from=rectangle]
\inheritanchor[from=rectangle]{center}
\inheritanchor[from=rectangle]{north}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{west}
\inheritanchor[from=rectangle]{east}
\backgroundpath{% this is new
% store lower left in xa/ya and upper right in xb/yb
\southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
\northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
\pgfmathsetmacro{\temp@x}{\pgf@xa+.5*(\pgf@xb-\pgf@xa)}
\pgfmathsetmacro{\ttemp@x}{.5*(\pgf@xb-\pgf@xa)} % construct main path
\pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
\pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yb}}
\pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
\pgfpathclose
\pgfpathmoveto{\pgfpoint{\temp@x}{\pgf@ya}}
\pgfpatharc{270}{-90}{\ttemp@x}
}
}
\makeatother
\begin{tikzpicture}[font=\sffamily]
\node [circle, draw, minimum size=2.5mm, inner sep=0pt] (t1) at (3,0) {};
\node [rectCircle, draw] (t5) at (0,2) {};
\node [circle, draw, minimum size=2.5mm, inner sep=0pt] (t2) at (0,3) {};
\node [circle, draw, minimum size=2.5mm, inner sep=0pt] (t4) at (2,2) {};
\node [rectCircle, draw] (t3) at (2,1) {};
\coordinate (orig) at (0,0);
\draw (orig) -- (t1) node [below=2mm] {$t_1$};
\draw [->] (t1) -- ++(0.5,0);
\draw (orig) -- (t5) node[left=2mm]{$t_5$} -- (t2) node[left=2mm] {$t_2$};
\draw [->] (t2) -- ++(0,0.5);
\draw [dashed] (t5) -- (t4) node[right=2mm] {$t_4$} -- (t3) node[right=2mm] {$t_3$};
\draw [dashed] (t2) -- ++(3,0) node {$\times$} -- (t1);
\end{tikzpicture}
\end{document}
生成的图像如下所示:
我认为最困难的部分是创建新的节点形状。这可能也可以通过使用并将节点绘制在彼此之上来完成overlay
。remember picture
但这需要非常精确的尺寸,并且不会像创建新形状那样容易扩展。创建图例留给读者练习,现在所有节点都可用,应该相对简单。
答案2
以下代码展示了创建形状的另一种方法rectCircle
。无需声明新形状,只需使用带有append after command
选项的样式即可。
cross
此外,还宣布了一种将取代$\times$
Roelof 解决方案的风格。
\documentclass[border=2mm,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.misc}
\begin{document}
\begin{tikzpicture}[font=\sffamily,
myCircle/.style={circle, draw, minimum size=2.5mm, inner sep=0pt},
rectCircle/.style={myCircle,
append after command={
\pgfextra
\draw (\tikzlastnode.north-|\tikzlastnode.west) rectangle
(\tikzlastnode.south-|\tikzlastnode.east);
\endpgfextra}},
cross/.style={cross out, minimum size=1.5mm, draw, inner sep=0pt, solid}
]
\node [myCircle] (t1) at (3,0) {};
\node [rectCircle, draw] (t5) at (0,2) {};
\node [myCircle] (t2) at (0,3) {};
\node [myCircle] (t4) at (2,2) {};
\node [rectCircle, draw] (t3) at (2,1) {};
\coordinate (orig) at (0,0);
\draw (orig) -- (t1) node [below=2mm] {$t_1$};
\draw [->] (t1) -- ++(0.5,0);
\draw (orig) -- (t5) node[left=2mm]{$t_5$} -- (t2) node[left=2mm] {$t_2$};
\draw [->] (t2) -- ++(0,0.5);
\draw [dashed] (t5) -- (t4) node[right=2mm] {$t_4$} --(t3) node[right=2mm] {$t_3$};
\draw [dashed] (t2) -- ++(3,0) node[cross] {} -- (t1);
\end{tikzpicture}
\end{document}