6

Table of Contents

  1. What I want to do
  2. What I've tried
    1. Lines that connect the middle points of the rectangle
    2. Lines that connect the corners of the rectangle
    3. Rotated rectangle in the center
    4. Putting it all together
  3. What I haven't been able to do
  4. Template for answer

What I want to do

I'm currently learning Chinese. I want to create my own cheatsheets for practicing handwriting of Chinese characters. I want to do the following graphic using TikZ.

enter image description here

What I've tried

Lines that connect the middle points of the rectangle

I've managed to show lines connecting the middle line of the rectangle.

\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}

enter image description here

Lines that connect the corners of the rectangle

I've also managed to show lines that connect the corners of the rectangle.

\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}

enter image description here

Rotated rectangle in the center

I've also managed to show a rotated rectangle inside the rectangle.

\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}

enter image description here

Putting it all together

\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}

enter image description here

What I haven't been able to do

I'm now trying to show a centered rectangle (please see image below, image was created using Inkscape), but I haven't been able to do it.

enter image description here

The closest I could get was to draw a centered circle. I could also draw a rectangle inside the rectangle, but wasn't correctly placed in the center.

\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}

enter image description here

How could I do this?

Template for answer

You might not have a Chinese font installed in your system. This is not really important for the answer. I'm mostly interested in getting the guides be correctly shown. The template below shows the character "a" instead of a Chinese character.

\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}

enter image description here

1
  • Looking at your total figure, that square is already there. Somehow naming those intersections as nodes and then connecting those nodes would be your answer. Unfortunately I haven’t used that library, so I can’t give you an answer …. Yet ..
    – alchemist
    Feb 7, 2023 at 20:24

1 Answer 1

6

You can use the calc library to compute midpoints of coordinates you already know.

enter image description here

The syntax is ($.5*(node1)+.5*(node2)$) for the midpoint.

\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}
2
  • 2
    I think that midpoint can be found like this too: ($(node1)!0.5!(node2)$).
    – SebGlav
    Feb 7, 2023 at 21:35
  • 1
    Good point @SebGlav.
    – Sandy G
    Feb 8, 2023 at 13:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .