Tikzpicture 和 draw 函数产生不均匀的线条

Tikzpicture 和 draw 函数产生不均匀的线条

在书籍文档类中,使用tikz包含以下库的包:matrix,shapes,arrows,positioning,chains,calc

在节点矩阵中,我看到 draw 函数生成的线不是直线。代码如下:

\documentclass{book}

\usepackage{tikz}

\usetikzlibrary{matrix,shapes,arrows,positioning,chains,calc}

\begin{document}

\begin{tikzpicture}

\matrix [matrix of math nodes,column sep=1cm,row sep=1cm]
        {
                |(A1)| & |(A2)| t & |(A3)| t \omega & |(A4)| t \omega^2\\
                |(B1)| s & |(B2)| s + t & |(B3)| s + t \omega & |(B4)| s + t \omega^2\\
                |(C1)| s \omega & |(C2)| s \omega + t & |(C3)| s \omega + t \omega & |(C4)| s \omega + t \omega^2\\
                |(D1)| s \omega^2 & |(D2)| s \omega^2 + t & |(D3)| s \omega^2 + t \omega & |(D4)| s \omega^2 + t \omega^2\\
};
        \begin{scope}[every node/.style={midway,auto,font=\scriptsize}]
                \draw[shorten <=-0.25cm,shorten >=-0.25cm] ($(A1)!0.5!(A2)$)--($(D1)!0.5!(D2)$);
                \draw[shorten <=-0.5cm,shorten >=-0.5cm] ($(A1)!0.5!(B1)$)--($(A4)!0.5!(B4)$);
                \draw [red, dashed] (B2) -- node {} (C4);
                \draw [red, dashed] (C4) -- node {} (D3);
                \draw [red, dashed] (B2) -- node {} (D3);
                \draw [blue, dashed] (B3) -- node {} (D4);
                \draw [blue, dashed] (D4) -- node {} (C2);
                \draw [blue, dashed] (C2) -- node {} (B3);
                \draw [green, dashed] (B4) -- node {} (C3);
                \draw [green, dashed] (C3) -- node {} (D2);
        \end{scope}

\end{tikzpicture}

\end{document}

A1 节点是空的(按设计)。但是,如果我在那里放一些文本,比如字母“x”,那么线就会变直。有没有办法把空白/虚拟占位符放在那个节点里?或者,有没有更好的方法来完全生成这个矩阵?我肯定没有正确理解 draw 函数。

顶线不均匀

答案1

我会按如下方式写出你的 MWE:

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{calc, matrix, quotes}

\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes, % added matrix name "m"
             nodes in empty cells, % added
             column sep=1cm, row sep=1cm]
{
            & t             & t\omega               & t \omega^2            \\[-0.5cm]
s           & s + t         & s + t\omega           & s + t \omega^2        \\
s\omega     & s\omega + t   & s\omega + t\omega     & s\omega + t\omega^2   \\
s\omega^2   & s\omega^2 + t & s\omega^2 + t\omega   & s\omega^2 + t\omega^2 \\
};
%  at all lines is used matrix cells naming system
\draw ($(m-1-1.north)!0.5!(m-1-2.north)$)--($(m-4-1.south)!0.5!(m-4-2.south)$);
\draw ($(m-1-1.west)!0.5!(m-2-1.west)$)--($(m-1-4.east)!0.5!(m-2-4.east)$);
\draw [red, dashed] 
    (m-2-2) -- (m-3-4)
    (m-3-4) -- (m-4-3)
    (m-2-2) -- (m-4-3);
\draw [blue, dashed] 
    (m-2-3) -- (m-4-4)
    (m-4-4) -- (m-3-2)
    (m-3-2) -- (m-2-3);
\draw [green, dashed] 
    (m-2-4) -- (m-3-3)
    (m-3-3) -- (m-4-2);
\end{tikzpicture}
\end{document}

这使:

在此处输入图片描述

不清楚为什么你要画线,例如\draw [green, dashed] (C3) -- node {} (D2);。添加空节点不会造成任何损害,但如果以后不添加一些文本,那么它就是多余的。在这种情况下,该库quotes 很方便:\draw [green, dashed] (C3) to["<text>"] (D2);

希望代码足够清晰易懂。

相关内容