我想在以下代码中将节点向前移动

我想在以下代码中将节点向前移动
\begin{tikzpicture}[scale=.8]
\coordinate (1) at (-1,1);
\coordinate (2) at (1,1);
\coordinate (3) at (1,-1);
\coordinate (4) at (-1,-1);
\coordinate (5) at (0,2.8);
\coordinate (6) at (2.8,0);
\coordinate (7) at (0,-2.8);
\coordinate (8) at (-2.8,0);
\fill[black!100, draw=black, thick] (1) circle (1.5pt) node[black, below right] {$x_1$};
\fill[black!100, draw=black, thick] (2) circle (1.5pt) node[black, below left] {$x_3$};
\fill[black!100, draw=black, thick] (3) circle (1.5pt) node[black, above left] {$x_5$};
\fill[black!100, draw=black, thick] (4) circle (1.5pt) node[black, above right] {$x_7$};
\fill[black!100, draw=black, thick] (5) circle (1.5pt) node[black, above] {$x_2$};
\fill[black!100, draw=black, thick] (6) circle (1.5pt) node[black, right] {$x_4$};
\fill[black!100, draw=black, thick] (7) circle (1.5pt) node[black, below] {$x_6$};
\fill[black!100, draw=black, thick] (8) circle (1.5pt) node[black, left] {$x_8$};
\draw[fill opacity=0.7,fill=gray] (1) -- (2) --  (5);
\draw[fill opacity=0.7,fill=gray] (2) -- (3) --  (6);
\draw[fill opacity=0.7,fill=gray] (3) -- (4) --  (7);
\draw[fill opacity=0.7,fill=gray] (4) -- (1) --  (8);
\draw[fill opacity=0.7,fill=gray] (2) -- (6) --  (5);
\draw[fill opacity=0.7,fill=gray] (3) -- (7) --  (6);
\draw[fill opacity=0.7,fill=gray] (4) -- (8) --  (7);
\draw[fill opacity=0.7,fill=gray] (1) -- (5) --  (8);
\end{tikzpicture}

答案1

另一种解决方案是使用两个方形节点代替特定坐标

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}
\node[draw, thick, fill=gray, fill opacity=0.7, rotate=45,
    label={north east:$x_2$}, 
    label={north west:$x_8$}, 
    label={south west:$x_6$}, 
    label={south east:$x_4$}, minimum size=4cm] (out) {};
\node[draw, thick, fill=white,
    label={[anchor=north west]north west:$x_1$}, 
    label={[anchor=north east]north east:$x_3$}, 
    label={[anchor=south east]south east:$x_5$},
    label={[anchor=south west]south west:$x_7$}, minimum size=2cm] (in) {};
\foreach \i in {south east, south west, north east, north west}
    \foreach \j in {in, out}
        \filldraw[black] (\j.\i) circle(2pt);
\draw (out.north east) edge (in.north west) edge (in.north east);
\draw (out.north west) edge (in.north west) edge (in.south west);
\draw (out.south west) edge (in.south west) edge (in.south east);
\draw (out.south east) edge (in.south east) edge (in.north east);
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

评论中已经给出了解决问题的建议,因此下面是如何为图像编写更简洁的代码的示例。其中考虑了上述评论:

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[
dot/.style = {circle, fill, inner sep=1.5pt,
              label=#1, node contents={}}
                    ]
\coordinate (1) at (-1,1);
\coordinate (2) at (1,1);
\coordinate (3) at (1,-1);
\coordinate (4) at (-1,-1);
\coordinate (5) at (0,2.8);
\coordinate (6) at (2.8,0);
\coordinate (7) at (0,-2.8);
\coordinate (8) at (-2.8,0);
\draw[fill opacity=0.7,fill=gray]
        (1) -- (2) --  (5)
        (2) -- (3) --  (6)
        (3) -- (4) --  (7)
        (4) -- (1) --  (8)
        (2) -- (6) --  (5)
        (3) -- (7) --  (6)
        (4) -- (8) --  (7)
        (1) -- (5) --  (8);
\path   (1) node[dot=below right: $x_1$]
        (2) node[dot=below left: $x_3$]
        (3) node[dot=above left: $x_5$]
        (4) node[dot=above right: $x_7$]
        (5) node[dot=above: $x_2$]
        (6) node[dot=right: $x_4$]
        (7) node[dot=below: $x_6$]
        (8) node[dot=left: $x_8$];
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容