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保持一致。註解掉這個環境將顯示角度標記巨集引入到邊界框的未對準。

相關內容