目录

目录

目录

  1. 我想做的事
  2. 我尝试过的方法
    1. 连接矩形中点的线
    2. 连接矩形角的线
    3. 中心旋转的矩形
    4. 综合起来
  3. 我没能做到的事
  4. 答案模板

我想做的事

我目前正在学习中文。我想创建自己的练习汉字书写的速记表。我想使用 TikZ 制作以下图形。

在此处输入图片描述

我尝试过的方法

连接矩形中点的线

我已设法显示连接矩形中线的线。

\documentclass{article}

\usepackage{fontspec}
\setmainfont{FandolKai}

\usepackage{tikz}

\tikzset{
  mysquare/.style = {
    draw,
    minimum height = 0.4cm,
    minimum width = 0.4cm,
    inner sep = 0pt,
    outer sep = 0pt,
  },
  MyBgMiddlePoints/.style = {
    path picture={
      \draw[
        gray,
        dashed,
        draw opacity = 0.4,
        line width = 0.5pt,
        dash pattern = on 1pt off 1pt,
      ]
      (path picture bounding box.north) -- (path picture bounding box.south)
      (path picture bounding box.west) -- (path picture bounding box.east);
    }
  }
}

\begin{document}
  \begin{figure}
  \centering
  \begin{tikzpicture}
    \coordinate(c0);
    \node[scale=3, mysquare, MyBgMiddlePoints] (n0) {好};
  \end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

连接矩形角的线

我还设法显示了连接矩形各个角的线。

\documentclass{article}

\usepackage{fontspec}
\setmainfont{FandolKai}

\usepackage{tikz}

\tikzset{
  mysquare/.style = {
    draw,
    minimum height = 0.4cm,
    minimum width = 0.4cm,
    inner sep = 0pt,
    outer sep = 0pt,
  },
  MyBgCorners/.style = {
    path picture={
      \draw[
        gray,
        dashed,
        draw opacity = 0.4,
        line width = 0.5pt,
        dash pattern = on 1pt off 1pt,
      ]
      (path picture bounding box.north west) -- (path picture bounding box.south east)
      (path picture bounding box.south west) -- (path picture bounding box.north east);
    }
  },
}

\begin{document}
  \begin{figure}
  \centering
  \begin{tikzpicture}
    \coordinate(c0);
    \node[scale=3, mysquare, MyBgCorners] (n0) {好};
  \end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

中心旋转的矩形

我还设法在矩形内显示一个旋转的矩形。

\documentclass{article}

\usepackage{fontspec}
\setmainfont{FandolKai}

\usepackage{tikz}

\tikzset{
  mysquare/.style = {
    draw,
    minimum height = 0.4cm,
    minimum width = 0.4cm,
    inner sep = 0pt,
    outer sep = 0pt,
  },
  MyBgRotatedRectangle/.style = {
    path picture={
      \draw[
        gray,
        dashed,
        draw opacity = 0.4,
        line width = 0.5pt,
        dash pattern = on 1pt off 1pt,
      ]
      (path picture bounding box.north) -- (path picture bounding box.east)
      (path picture bounding box.east) -- (path picture bounding box.south)
      (path picture bounding box.south) -- (path picture bounding box.west)
      (path picture bounding box.west) -- (path picture bounding box.north);
    }
  }
}

\begin{document}
  \begin{figure}
  \centering
  \begin{tikzpicture}
    \coordinate(c0);
    \node[scale=3, mysquare, MyBgRotatedRectangle] (n0) {好};
  \end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

综合起来

\documentclass{article}

\usepackage{fontspec}
\setmainfont{FandolKai}

\usepackage{tikz}

\tikzset{
  mysquare/.style = {
    draw,
    minimum height = 0.4cm,
    minimum width = 0.4cm,
    inner sep = 0pt,
    outer sep = 0pt,
  },
  MyBgMiddlePointsAndCornersAndRotatedRectangle/.style = {
    path picture={
      \draw[
        gray,
        dashed,
        draw opacity = 0.4,
        line width = 0.5pt,
        dash pattern = on 1pt off 1pt,
      ]
      %% Middle points
      (path picture bounding box.north) -- (path picture bounding box.south)
      (path picture bounding box.west) -- (path picture bounding box.east)
      %% Corners
      (path picture bounding box.north west) -- (path picture bounding box.south east)
      (path picture bounding box.south west) -- (path picture bounding box.north east)
      %% Rotated rectangle
      (path picture bounding box.north) -- (path picture bounding box.east)
      (path picture bounding box.east) -- (path picture bounding box.south)
      (path picture bounding box.south) -- (path picture bounding box.west)
      (path picture bounding box.west) -- (path picture bounding box.north);
    }
  }
}

\begin{document}
  \begin{figure}
  \centering
  \begin{tikzpicture}
    \coordinate(c0);
    \node[scale=3, mysquare, MyBgMiddlePointsAndCornersAndRotatedRectangle] (n0) {好};
  \end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

我没能做到的事

我现在正尝试显示一个居中的矩形(请参见下图,图像是使用 Inkscape 创建的),但我无法做到这一点。

在此处输入图片描述

我能做到的最接近的就是画一个居中的圆。我也可以在矩形内画一个矩形,但没有正确地放置在中心。

\documentclass{article}

\usepackage{fontspec}
\setmainfont{FandolKai}

\usepackage{tikz}
%% The following tikzlibrary is used for using the "regular polygon"
%% and "regular polygon sides" commands which I used when trying to
%% draw a rectangle inside a rectangle
\usetikzlibrary{shapes.geometric}

\tikzset{
  mysquare/.style = {
    draw,
    minimum height = 0.4cm,
    minimum width = 0.4cm,
    inner sep = 0pt,
    outer sep = 0pt,
  },
  MyBgLinesInMiddlePoints/.style = {
    path picture={
      \draw[
        gray,
        draw opacity = 0.4,
        line width = 1pt,
        dash pattern = on 1pt off 1pt,
      ]
      (path picture bounding box.north) -- (path picture bounding box.south)
      (path picture bounding box.west) -- (path picture bounding box.east);
    }
  },
  MyCircleInTheMiddle/.style = {
    path picture={
      \filldraw[
        gray,
        fill=white,
        draw opacity=0.4,
        line width = 1pt,
        dash pattern = on 1pt off 1pt
      ] (path picture bounding box.center) circle (0.15cm);
    }
  },
  MyRectangleInTheMiddle/.style = {
    path picture={
      \node[
        draw,
        line width = 1pt,
        regular polygon,
        regular polygon sides = 4,
      ] at (path picture bounding box.center) {};
    }
  },
}

\begin{document}
  \begin{figure}
  \centering
  \begin{tikzpicture}
    \coordinate(c0);
    \node[scale=3, mysquare, MyBgLinesInMiddlePoints] (n0) {好};
    \coordinate(c1) at (n0.east);
    \node[scale=3, anchor=west, mysquare, MyCircleInTheMiddle] (n1) at (c1) {好};
    %% This is my attempt
    \coordinate(c2) at (n1.east);
    \node[scale=3, anchor=west, mysquare, MyRectangleInTheMiddle] (n2) at (c2) {好};
  \end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

我怎样才能做到这一点?

答案模板

您的系统中可能没有安装中文字体。这对于答案来说并不重要。我最感兴趣的是让指南正确显示。下面的模板显示字符“a”而不是中文字符。

\documentclass{article}

\usepackage{tikz}

\tikzset{
  mysquare/.style = {
    draw,
    minimum height = 0.4cm,
    minimum width = 0.4cm,
    inner sep = 0pt,
    outer sep = 0pt,
  },
  MyBgMiddlePoints/.style = {
    path picture={
      \draw[
        gray,
        dashed,
        draw opacity = 0.4,
        line width = 1pt,
        dash pattern = on 1pt off 1pt,
      ]
      (path picture bounding box.north) -- (path picture bounding box.south)
      (path picture bounding box.west) -- (path picture bounding box.east);
    }
  },
}

\begin{document}
  \begin{figure}
  \centering
  \begin{tikzpicture}
    \coordinate(c0);
    \node[scale=3, mysquare, MyBgMiddlePoints] (n0) {a};
  \end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

答案1

您可以使用该calc库来计算您已经知道的坐标的中点。

在此处输入图片描述

该语法($.5*(node1)+.5*(node2)$)适用于中点。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\tikzset{
  mysquare/.style = {
    draw,
    minimum height = 0.4cm,
    minimum width = 0.4cm,
    inner sep = 0pt,
    outer sep = 0pt,
  },
  MyBgMiddlePoints/.style = {
    path picture={
      \draw[
        gray,
        dashed,
        draw opacity = 0.4,
        line width = 1pt,
        dash pattern = on 1pt off 1pt,
      ]
      (path picture bounding box.north) -- (path picture bounding box.south)
      (path picture bounding box.west) -- (path picture bounding box.east)
      (path picture bounding box.north east) -- (path picture bounding box.south west)
      (path picture bounding box.north west) -- (path picture bounding box.south east)
      (path picture bounding box.north) -- (path picture bounding box.east) -- (path picture bounding box.south) -- (path picture bounding box.west) -- (path picture bounding box.north)
      ($.5*(path picture bounding box.north west)+.5*(path picture bounding box)$)
        --($.5*(path picture bounding box.north east)+.5*(path picture bounding box)$)
        --($.5*(path picture bounding box.south east)+.5*(path picture bounding box)$)
        --($.5*(path picture bounding box.south west)+.5*(path picture bounding box)$)
        --($.5*(path picture bounding box.north west)+.5*(path picture bounding box)$);
    }
  },
}

\begin{document}
  \begin{figure}
  \centering
  \begin{tikzpicture}
    \coordinate(c0);
    \node[scale=3, mysquare, MyBgMiddlePoints] (n0) {a};
  \end{tikzpicture}
\end{figure}

\end{document}

相关内容