TikZ:将图片置于中心

TikZ:将图片置于中心

我的代码无法对齐绘图,因此 SR 线(图表摘录的中心)位于页面中间。与上方的文本以及绘图下方的标题和分页相比,绘图明显向左移动。

\documentclass[12pt, a4paper, oneside]{article}
\usepackage{indentfirst}
\usepackage[margin = 2.5cm]{geometry}
\usepackage{tkz-euclide}
\usepackage{amsmath}
\usepackage{amssymb}
\usetkzobj{all}

\begin{document}

...some text here...

\begin{align*}
    \sec{\varphi}=\frac{|OS|}{|OR|}=\frac{|OS|}{1}=|OS|
& & \cot{\varphi}=\frac{|OU|}{|OT|}=\frac{|OU|}{1}=|OU|
\end{align*}

{\centering

\begin{tikzpicture}[scale=4]

    \def\costhirty{0.8660256}
    \def\secthirty{1.1547}
    \def\sinfifteen{-0.258819}
    \def\cosfifteen{0.9659258}

    \draw[color=gray,dotted,step=1cm] (-0.25,-0.25) grid (2.25,1.25);   

    \draw (\cosfifteen,\sinfifteen) arc (-15:105:1cm);

    \draw[color=gray,dotted] (0,0) -- (30:2.5cm);
    \draw[color=gray,dotted] (30:1cm) -- +(-90:0.75cm);
    \draw[color=gray,dotted] (30:1cm) -- +(90:0.75cm);

    \coordinate (O) at (0, 0);
    \coordinate (P) at (\costhirty, 0);
    \coordinate (Q) at (30:1cm);
    \coordinate (R) at (1, 0);
    \coordinate (S) at (30:\secthirty);
    \coordinate (T) at (0, 1);
    \coordinate (U) at (30:2);

    \draw[thick] (O)--(90:1.25);
    \draw[thick] (O)--(0:2.25);

    \draw[thick] (Q)--(P);
    \draw[thick] (O)--(U)--(T);
    \draw[thick] (R)--(S);

    \tkzLabelPoints[below left](O)
    \tkzLabelPoints[below left](P)
    \tkzLabelPoints[left = 5pt of Q](Q)
    \tkzLabelPoints[below right](R)
    \tkzLabelPoints[below right](S)
    \tkzLabelPoints[above left](T)
    \tkzLabelPoints[above](U)

    \tkzMarkAngle[fill = gray, size=0.3cm, opacity = .3](P,O,Q)
    \tkzLabelAngle[pos = 0.2](P,O,Q){$\varphi$}

    \tkzMarkAngle[fill = gray, size=0.3cm, opacity = .3](T,U,O)
    \tkzLabelAngle[pos = -0.2](O,U,T){$\varphi$}

\end{tikzpicture}

\textit{\scriptsize{Figure 2}}

}

\end{document}

上述代码的预览效果如下。(双红线是后来添加的。)

绘画

我怎样才能使 SR 线居中,与穿过双红线的其余内容对齐?

答案1

我不知道具体原因,但tkz-euclide角度标记宏必须使用一些超出图形边界框的内部控制点。

解决方案是将它们封装在pgfinterruptboundingbox环境中:

\documentclass[12pt, a4paper, oneside]{article}
\usepackage{indentfirst}
\usepackage[margin = 2.5cm]{geometry}
\usepackage{tkz-euclide}
\usepackage{amsmath}
\usepackage{amssymb}
\usetkzobj{all}

\begin{document}

...some text here...

\begin{align*}
    \sec{\varphi}=\frac{|OS|}{|OR|}=\frac{|OS|}{1}=|OS|
& & \cot{\varphi}=\frac{|OU|}{|OT|}=\frac{|OU|}{1}=|OU|
\end{align*}

{\centering

\begin{tikzpicture}[scale=4]

    \def\costhirty{0.8660256}
    \def\secthirty{1.1547}
    \def\sinfifteen{-0.258819}
    \def\cosfifteen{0.9659258}

    \draw[color=gray,dotted,step=1cm] (-0.25,-0.25) grid (2.25,1.25);   

    \draw (\cosfifteen,\sinfifteen) arc (-15:105:1cm);

    \draw[color=gray,dotted] (0,0) -- (30:2.5cm);
    \draw[color=gray,dotted] (30:1cm) -- +(-90:0.75cm);
    \draw[color=gray,dotted] (30:1cm) -- +(90:0.75cm);

    \coordinate (O) at (0, 0);
    \coordinate (P) at (\costhirty, 0);
    \coordinate (Q) at (30:1cm);
    \coordinate (R) at (1, 0);
    \coordinate (S) at (30:\secthirty);
    \coordinate (T) at (0, 1);
    \coordinate (U) at (30:2);

    \draw[thick] (O)--(90:1.25);
    \draw[thick] (O)--(0:2.25);

    \draw[thick] (Q)--(P);
    \draw[thick] (O)--(U)--(T);
    \draw[thick] (R)--(S);

    \tkzLabelPoints[below left](O)
    \tkzLabelPoints[below left](P)
    \tkzLabelPoints[left = 5pt of Q](Q)
    \tkzLabelPoints[below right](R)
    \tkzLabelPoints[below right](S)
    \tkzLabelPoints[above left](T)
    \tkzLabelPoints[above](U)
    \begin{pgfinterruptboundingbox}
        \tkzMarkAngle[fill = gray, size=0.3cm, opacity = .3](P,O,Q)
        \tkzLabelAngle[pos = 0.2](P,O,Q){$\varphi$}
        \tkzMarkAngle[fill = gray, size=0.3cm, opacity = .3](T,U,O)
        \tkzLabelAngle[pos = -0.2](O,U,T){$\varphi$}
    \end{pgfinterruptboundingbox}
\end{tikzpicture}

\textit{\scriptsize{Figure 2}}

}

\end{document}

在此处输入图片描述


稍微缩小示例,使用一些测试“仪器”可以更清楚地显示问题:

\documentclass{article}
\usepackage{tkz-euclide}
\usepackage{amsmath}
\usetkzobj{all}

\begin{document}

...some text here... \hrulefill

\begin{align*}
    \sec{\varphi}=\frac{|OS|}{|OR|}=\frac{|OS|}{1}=|OS|
& & \cot{\varphi}=\frac{|OU|}{|OT|}=\frac{|OU|}{1}=|OU|
\end{align*}

{\centering

\begin{tikzpicture}[scale=4]
    \coordinate (O) at (0, 0);
    \coordinate (P) at ({cos(30)}, 0);
    \coordinate (Q) at (30:1cm);
    \coordinate (R) at (1, 0);
    \coordinate (S) at (30:{sec(30)});
    \coordinate (T) at (0, 1);
    \coordinate (U) at (30:2);

    \draw[thick] (Q)--(P);
    \draw[thick] (O)--(U)--(T);
    \draw[thick] (R)--(S);

    \node[red] at (current bounding box.south) {$\vert$};
    \begin{pgfinterruptboundingbox}
        \tkzMarkAngle[fill = gray, size=0.3cm, opacity = .3](P,O,Q)
        \tkzLabelAngle[pos = 0.2](P,O,Q){$\varphi$}
        \tkzMarkAngle[fill = gray, size=0.3cm, opacity = .3](T,U,O)
        \tkzLabelAngle[pos = -0.2](O,U,T){$\varphi$}
    \end{pgfinterruptboundingbox}
    \node[blue] at (current bounding box.south) {$\vert$};
\end{tikzpicture}

$\vert$

}

\end{document}

在此处输入图片描述

注意,在pgfinterruptboundingbox环境中,所有三个\vert都是对齐的。注释掉此环境将显示角度标记宏引入边界框的错位。

相关内容