![如何在此 tikz 代码中移动圆圈的位置](https://linux22.com/image/358291/%E5%A6%82%E4%BD%95%E5%9C%A8%E6%AD%A4%20tikz%20%E4%BB%A3%E7%A0%81%E4%B8%AD%E7%A7%BB%E5%8A%A8%E5%9C%86%E5%9C%88%E7%9A%84%E4%BD%8D%E7%BD%AE.png)
我想减少此代码中标题和圆圈之间的间距。我还需要一些建议来提高此代码的效率
\documentclass{article} \usepackage{tikz} \usetikzlibrary{angles,quotes}
\def\myrad{3cm}% radius of the circle \def\myang{60}% angle for the arc
\begin{document}
\def\myrad{1.5cm}% radius of the circle
\def\myang{45}% angle for the arc
\begin{tikzpicture}
% the origin
\coordinate (O) at (0,0);
% the box for deterministic part
\draw [fill=blue!05!white,dashed] (-4,-4) rectangle (4,4);
\draw [draw=none,node font=\LARGE] (-4,3) rectangle (4,4) node[midway]
{Deterministic};
% the circle and the dot at the origin
\draw (O) node[circle,inner sep=1.5pt,fill] {};
\draw [black!25,thick] circle [radius=\myrad];
% the ``\theta'' arc
\draw (\myrad,0) coordinate (xcoord) --
node[midway,below] {$r=1$} (O) --
(\myang:\myrad) coordinate (slcoord)
pic [draw,thick,->,>=stealth,angle radius=1cm,"$\theta$"] {angle = xcoord--O--slcoord};
% the outer ``s'' arc
\draw (\myrad,0) arc[start angle=0,end angle=\myang,radius=\myrad] {};
\node[draw=none,text width=4cm, font=\Large,align=center] at (0.25,-3){$0\leq \boldsymbol{\theta} \leq \pi/4$\vspace{2mm}$x=\cos(\theta_i)$\\$y=\sin(\theta_i)$};
\end{document}
答案1
您的 MWE 存在一些错误:
- 包裹丢失
amsmat
或mathtools
- 丢失的
\end{tikzpicture}
通过添加tikz
库backgrounds
,fit
我positioning
将scopes
您的 MWE 重写了如下:
\documentclass{article}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{angles, backgrounds, fit, quotes, positioning, scopes}
\begin{document}
\begin{center}
\begin{tikzpicture}[
node distance = 5mm,
my angle/.style = {draw, thick, -stealth,
angle radius=1cm,"$\theta$"}
]
\def\myrad{1.5cm}% radius of the circle
\def\myang{45}% angle for the arc
% the origin
\node (C) [circle,draw=red, minimum size=2*\myrad] {};
\node [circle, inner sep=1pt, fill] {};
% angle
\draw[thick]
(C.center) -- node[below] {$r=1$} (0:\myrad) coordinate (A)
arc (0:45:1.5) coordinate (B)
-- cycle;
\draw pic [my angle] {angle = A--C--B};
% box for deterministic part
\node (D) [above=of C, font=\Large] {Deterministic};
% math
\node (E) [below=of C, font=\Large] {%
$\begin{aligned}
0 & \boldsymbol{\theta} \leq \pi/4 \\
x & = \cos(\theta_i) \\
y & =\sin(\theta_i)
\end{aligned}$};
% background rectangle
\scoped[on background layer]
\node [draw, dashed, fill=gray!20, inner xsep=11mm,
fit=(C) (D) (E)] {};
\end{tikzpicture}
\end{center}
\end{document}
现在,所有图像元素的位置都是相对的,因此您可以集中控制节点之间的距离(按node distance
)和图像元素的大小。上面的代码可能比您的 MWE 中的代码略短。
我不清楚为什么方程式中的 用\theta
粗体显示,而图像中却没有。我认为你应该删除boldsymbol
方程式中的 。方程式现在位于amsmath
( mathtools
) 环境中aligned
。我更喜欢居中,而不是删除方程式中的 & 符号并aligned
替换为gathered
。
如您所见,图像的枢轴是圆C
。然后相对于它绘制圆“剁碎”和角度,上面是图像标题,下面是方程式。背景矩形是真的背景并最后绘制。
附录: 为圆圈和角度区域添加了颜色。希望我理解正确 :)
\documentclass{article}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{angles, backgrounds, fit, quotes, positioning, scopes}
\begin{document}
\begin{center}
\begin{tikzpicture}[
node distance = 5mm,
my angle/.style = {draw, thick, -stealth,
angle radius=1cm,"$\theta$"}
]
\def\myrad{1.5cm}% radius of the circle
\def\myang{45}% angle for the arc
% the origin
\node (C) [circle, draw=red, fill=red!10, minimum size=2*\myrad] {};% added fill, color is red!10. you can change color according to your taste/wish
\node [circle, inner sep=1pt, fill] {};
% angle
\draw[thick, fill=white] % added fill, color is white. you can change color according to your taste/wish
(C.center) -- node[below] {$r=1$} (0:\myrad) coordinate (A)
arc (0:45:1.5) coordinate (B)
-- cycle;
\draw pic [my angle] {angle = A--C--B};
% box for deterministic part
\node (D) [above=of C, font=\Large] {Deterministic};
% math
\node (E) [below=of C, font=\Large] {%
$\begin{aligned}
0 & \leq \boldsymbol{\theta} \leq \pi/4 \\
x & = \cos(\theta_i) \\
y & = \sin(\theta_i)
\end{aligned}$};
% background rectangle
\scoped[on background layer]
\node [draw, dashed, fill=blue!10, inner xsep=11mm,
fit=(C) (D) (E)] {};
\end{tikzpicture}
\end{center}
\end{document}