TiKz 中的白日梦

TiKz 中的白日梦

我正在寻找一个简化白日梦绘图的宏(这些是组合学中使用的特定平铺)。但是我还没有找到。所以我自己写了它,并在下面的答案中提供了可能的解决方案。

答案1

这是我之前为制作结状马赛克而编写的代码。它可能需要更新,但对于您的目的来说已经足够了。

在此处输入图片描述

% mosaic         New environment for classical (unlabeled) knot mosaics. Usage is as follows:
%                   \begin{mosaic}[<tikz options>]{<tile size in cm>} 
%                      <entry> \& <entry> \& ... \& <entry> \\
%                      <entry> \& <entry> \& ... \& <entry> \\
%                        ...
%                      <entry> \& <entry> \& ... \& <entry> \\
%                   \end{mosaic}
%                Final \\ in last line is required.
%                Optional <size> has default 1. Unit is cm. Determines size of each tile.
%                Each <entry> is typically a tilename but can be anything or empty. Note that
%                <entries> must be separated by \& not just with & (as for a typical table).

\documentclass{article}

\usepackage{mathtools}
\usepackage{tikz,environ}

\NewEnviron{mosaic}[2][]{\begin{tikzpicture}[baseline]\def\tilesize{#2}\matrix[mosai,ampersand replacement=\&, #1]{\BODY};\end{tikzpicture}}

\newcommand{\cspace}{.1} % spacing for crossings
\newcommand{\tileedge}{.05pt} % thickness of tile edge. Does not scale.
\newcommand{\knotthickness}{1.5pt} % thickness of knot for 1cm tile. Scales with tile size.

\newcommand{\tileo}{\tikz \draw[tilestyle] (0,0) rectangle (\tilesize,\tilesize);}
\newcommand{\tilei}[1][{}]{\tikz {\draw[tilestyle] (0,0) rectangle (\tilesize,\tilesize); \draw[#1,knotstyle={\tilesize*\knotthickness}] (0,.5*\tilesize) to[out=0, in=90] (.5*\tilesize,0);}}
\newcommand{\tileii}[1][{}]{\tikz {\draw[tilestyle] (0,0) rectangle (\tilesize,\tilesize); \draw[#1,knotstyle={\tilesize*\knotthickness}] (\tilesize,.5*\tilesize) to[out=180, in=90] (.5*\tilesize,0);}}
\newcommand{\tileiii}[1][{}]{\tikz {\draw[tilestyle] (0,0) rectangle (\tilesize,\tilesize); \draw[#1,knotstyle={\tilesize*\knotthickness}] (\tilesize,.5*\tilesize) to[out=180, in=-90] (.5*\tilesize,\tilesize);}}
\newcommand{\tileiv}[1][{}]{\tikz {\draw[tilestyle] (0,0) rectangle (\tilesize,\tilesize); \draw[#1,knotstyle={\tilesize*\knotthickness}] (0,.5*\tilesize) to[out=0, in=-90] (.5*\tilesize,\tilesize);}}
\newcommand{\tilev}[1][{}]{\tikz {\draw[tilestyle] (0,0) rectangle (\tilesize,\tilesize); \draw[#1,knotstyle={\tilesize*\knotthickness}] (0,.5*\tilesize) to (\tilesize,.5*\tilesize);}}
\newcommand{\tilevi}[1][{}]{\tikz {\draw[tilestyle] (0,0) rectangle (\tilesize,\tilesize); \draw[#1,knotstyle={\tilesize*\knotthickness}] (.5*\tilesize,0) to (.5*\tilesize,\tilesize);}}
\newcommand{\tilevii}[1][{}]{\tikz {\draw[tilestyle] (0,0) rectangle (\tilesize,\tilesize); \draw[#1,knotstyle={\tilesize*\knotthickness}] (0,.5*\tilesize) to[out=0, in=90] (.5*\tilesize,0); \draw[#1,knotstyle={\tilesize*\knotthickness}] (\tilesize,.5*\tilesize) to[out=180, in=-90] (.5*\tilesize,\tilesize);}}
\newcommand{\tileviii}[1][{}]{\tikz {\draw[tilestyle] (0,0) rectangle (\tilesize,\tilesize); \draw[#1,knotstyle={\tilesize*\knotthickness}] (\tilesize,.5*\tilesize) to[out=180, in=90] (.5*\tilesize,0); \draw[#1,knotstyle={\tilesize*\knotthickness}] (0,.5*\tilesize) to[out=0, in=-90] (.5*\tilesize,\tilesize);}}
\newcommand{\tileix}[1][{}]{\tikz {\draw[tilestyle] (0,0) rectangle (\tilesize,\tilesize); \draw[#1,knotstyle={\tilesize*\knotthickness}] (0,.5*\tilesize) to (\tilesize,.5*\tilesize); \draw[#1,knotstyle={\tilesize*\knotthickness}] (.5*\tilesize,0) to (.5*\tilesize,.5*\tilesize-\tilesize*\cspace); \draw[#1,knotstyle={\tilesize*\knotthickness}] (.5*\tilesize,.5*\tilesize+\tilesize*\cspace) to (.5*\tilesize,\tilesize);}}
\newcommand{\tilex}[1][{}]{\tikz {\draw[tilestyle] (0,0) rectangle (\tilesize,\tilesize); \draw[#1,knotstyle={\tilesize*\knotthickness}] (.5*\tilesize,0) to (.5*\tilesize,\tilesize); \draw[#1,knotstyle={\tilesize*\knotthickness}] (0,.5*\tilesize) to (.5*\tilesize-\tilesize*\cspace,.5*\tilesize); \draw[#1,knotstyle={\tilesize*\knotthickness}] (.5*\tilesize+\tilesize*\cspace,.5*\tilesize) to (\tilesize,.5*\tilesize);}}

\tikzset{tilestyle/.style={use as bounding box, line width=\tileedge}} % Add colors/styles here for tile edge and fill. Example: fill=gray!30 for light gray background
\tikzset{knotstyle/.style={line width=#1}} % Add colors and other line styles here
\tikzset{mosai/.style={execute at begin cell=\node\bgroup, execute at end cell=\egroup;, column sep={\tilesize cm,between origins}, row sep={\tilesize cm,between origins}, inner sep=0}}


\begin{document}

This is a displayed classical mosaic with tiles $T_0$--$T_{10}$ running across the rows. Classical mosaics need not be square. Empty entries leave empty mosaic tiles, i.e., no border square.

\[
\begin{mosaic}{1}
    \tileo \& \tilei \& \tileii \& \tileiii\\
    \tileiv \& \tilev \& \tilevi \& \tilevii\\
    \tileviii \& \tileix \& \tilex \\
\end{mosaic}
\]
\vspace{2cm}

\[
\begin{mosaic}[font=\Large\sffamily]{1}
  \& 1 \& 2\& 3 \& 4 \&5 \& 6\\
1 \& \tilex \& \tilex \& \tileviii \& \tileviii \& \tileviii \& \tileiv\\
2 \& \tileviii \& \tileviii \& \tileviii \& \tileviii \& \tileiv\\
3 \& \tilex \& \tilex \& \tilex \& \tileiv\\
4 \& \tilex \& \tileviii \& \tileiv\\
5 \& \tileviii \& \tileiv\\
6 \& \tileiv\\
\end{mosaic}
\]

\end{document}

答案2

这是一个简单的解决方案。(它不是最有效的,但我仍在努力)。


\documentclass{minimal}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{bm}
\newcommand{\cross}[2]{
\begin{scope}[xshift=#1cm,yshift=-#2cm]
\draw[very thick] (0,-0.5) -- (0.45,-0.5);
\draw[very thick] (0.55,-0.5) -- (1,-0.5);
    \draw[very thick] (0.5,0) -- (0.5,-1);
\end{scope}
}%draws a cross
\newcommand{\arcs}[2]{
\begin{scope}[xshift=#1cm,yshift=-#2cm]
    \draw[very thick] (0,-0.5) arc (-90:0:0.5cm);
    \draw[very thick] (0.5,-1) arc (180:90:0.5cm);
\end{scope}
}%draws elbows (arcs)
\newcommand{\GRID}[1]{
\begin{scope}
\draw[black!25!white] (0,-#1cm) |- (#1cm,0);
\foreach \n in {0,...,{#1}} \draw[black!25!white] (\n+1,0) -- (\n+1,\n-#1);
\foreach \n in {0,...,{#1}} \draw[black!25!white] (0,-\n-1) -- (#1-\n,-\n-1);
\foreach \n in {1,...,{#1}} \draw[very thick] (\n-1,-#1+\n-0.5) arc (-90:0:0.5cm);
\end{scope}
}%draws a grid and the outer elbows

\newcommand{\GRIDwitnum}[1]{
\begin{scope}
\foreach \n in {1,...,#1} \node at (\n-0.5,0.5){$\bm{\mathsf{\n}}$};
\foreach \n in {1,...,#1} \node at (-0.5,-\n+0.5){$\bm{\mathsf{\n}}$};
\draw[black!25!white] (0,-#1cm) |- (#1cm,0);
\foreach \n in {0,...,{#1}} \draw[black!25!white] (\n+1,0) -- (\n+1,\n-#1);
\foreach \n in {0,...,{#1}} \draw[black!25!white] (0,-\n-1) -- (#1-\n,-\n-1);
\foreach \n in {1,...,{#1}} \draw[very thick] (\n-1,-#1+\n-0.5) arc (-90:0:0.5cm);
\end{scope}
} %draws a grid and the outer elbows and numerates the pipe’ ends 
\begin{document}

\begin{tikzpicture}[scale=0.6]
    \GRIDwitnum{6}
    \cross{0}{0}
    \cross{1}{0}
    \arcs{2}{0}
    \arcs{3}{0}
    \arcs{4}{0}
    \arcs{0}{1}
    \arcs{1}{1}
    \arcs{2}{1}
    \arcs{3}{1}
    \cross{0}{2}
    \cross{1}{2}
    \cross{2}{2}
    \cross{0}{3}
    \arcs{1}{3}
    \arcs{0}{4}
\end{tikzpicture}

\end{document}

看起来像这样!

相关内容