我需要绘制一些重叠的矩形组,例如:
它们用数字标记,矩形 1 与矩形 2 重叠,等等。
有没有办法自动改变数字的位置,让它们全部可见?或者将所有数字放在所有矩形上?
代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,backgrounds}
\usepackage{etoolbox}% provides \preto
\makeatletter
\newcommand*{\collect@draws}{}
\newcommand*{\reversedraws}{%
\renewcommand*{\collect@draws}{}% initialize
\look@for@draw
}
\newcommand*{\look@for@draw}{%
\@ifnextchar\draw{%
\catch@draw
}{%
\collect@draws % output the result
}%
}
\def\catch@draw\draw#1;{%
\preto\collect@draws{\draw#1;}%
\look@for@draw
}
\makeatother
\begin{document}
\begin{tikzpicture}[scale = 4]
\reversedraws
\draw [fill=yellow] (0.445041,0.801937) rectangle (0,1) node[midway] {$1$};
\draw [fill=yellow] (0.692021,0.356896) rectangle (1,1) node[midway] {$2$};
\draw [fill=yellow] (0.692021,0.356895) rectangle (0,0) node[midway] {$3$};
\draw [fill=yellow] (0.137063,0.158832) rectangle (0,1) node[midway] {$4$};
\draw [fill=yellow] (0.384043,0.713791) rectangle (1,0) node[midway] {$5$};
\draw [fill=yellow] (0.829084,0.515727) rectangle (0,1) node[midway] {$6$};
\draw [fill=yellow] (0.521105,0.872622) rectangle (0,0) node[midway] {$7$};
\end{tikzpicture}
\begin{tikzpicture}[scale = 4]
\reversedraws
\draw [fill=yellow] (0.145041,0.801937) rectangle (0,1) node[midway] {$1$};
\draw [fill=yellow] (0.692021,0.356896) rectangle (0.7,1) node[midway] {$2$};
\draw [fill=yellow] (0.392021,0.356895) rectangle (0,0) node[midway] {$3$};
\draw [fill=yellow] (0.384043,0.713791) rectangle (0.7,0) node[midway] {$4$};
\draw [fill=yellow] (0.529084,0.515727) rectangle (0,1) node[midway] {$5$};
\draw [fill=yellow] (0.521105,0.872623) rectangle (0.7,1) node[midway] {$6$};
\draw [fill=yellow] (0.221105,0.872622) rectangle (0,0) node[midway] {$7$};
\draw [fill=yellow] (0.213127,0.229518) rectangle (0.7,1) node[midway] {$8$};
\end{tikzpicture}
\end{document}
答案1
自动更改节点的位置可能很难,但如果您事先知道所有情况,则可以做到。我不确定这是否值得。
但是,您可以将节点放在前台,而且它们需要有背景(以覆盖后面的线条),我认为这是根据当前信息我能提出的最佳建议。
您可以添加[mid]
到您想要的前台节点,但如果您希望将其应用于所有节点,只需mid
在其中every node
一个节点中包含样式即可。尽管我建议不要这样做,因为您可能会影响其他最好不要管的节点(例如本例中的节点 6)。
选择
every node/.style={midway},
every path/.style={fill=yellow},
分别适用于所有节点和所有路径。因此您无需在代码中指定这些。通常,最好为重复多次的事情设置一般规则。
这是来自的修改解决方案Loop Space 的答案。
输出
代码
\documentclass{article}
\usepackage{tikz}
\usepackage{etoolbox}% provides \preto
\usetikzlibrary{shapes,backgrounds}
\makeatletter
\newcommand*{\collect@draws}{}
\newcommand*{\reversedraws}{%
\renewcommand*{\collect@draws}{}% initialize
\look@for@draw
}
\newcommand*{\look@for@draw}{%
\@ifnextchar\draw{%
\catch@draw
}{%
\collect@draws % output the result
}%
}
\def\catch@draw\draw#1;{%
\preto\collect@draws{\draw#1;}%
\look@for@draw
}
\makeatother
%%%% Declaring layers
\pgfdeclarelayer{foreground}
\pgfsetlayers{main,foreground}
%%%% The node on layer definition
\makeatletter
\pgfkeys{%
/tikz/node on layer/.code={
\gdef\node@@on@layer{%
\setbox\tikz@tempbox=\hbox\bgroup\pgfonlayer{#1}\unhbox\tikz@tempbox\endpgfonlayer\egroup}
\aftergroup\node@on@layer
}}
\def\node@on@layer{\aftergroup\node@@on@layer}
\makeatother
%%%% Options
\tikzset{
every node/.style={midway},
every path/.style={fill=yellow},
mid/.style={midway, inner sep=2pt, outer sep=0,minimum size=1mm, fill=yellow,node on layer=foreground},
}
\begin{document}
\begin{tikzpicture}[scale = 4]
\reversedraws
\draw (0.445041,0.801937) rectangle (0,1) node {$1$};
\draw (0.692021,0.356896) rectangle (1,1) node {$2$};
\draw (0.692021,0.356895) rectangle (0,0) node {$3$};
\draw (0.137063,0.158832) rectangle (0,1) node {$4$};
\draw (0.384043,0.713791) rectangle (1,0) node[mid] {$5$};
\draw (0.829084,0.515727) rectangle (0,1) node {$6$};
\draw (0.521105,0.872622) rectangle (0,0) node {$7$};
\end{tikzpicture}
\end{document}
答案2
xshift
您可以使用和选项移动标签yshift
(参见带有数字的矩形,5
如标签)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,backgrounds}
\usepackage{etoolbox}% provides \preto
\makeatletter
\newcommand*{\collect@draws}{}
\newcommand*{\reversedraws}{%
\renewcommand*{\collect@draws}{}% initialize
\look@for@draw
}
\newcommand*{\look@for@draw}{%
\@ifnextchar\draw{%
\catch@draw
}{%
\collect@draws % output the result
}%
}
\def\catch@draw\draw#1;{%
\preto\collect@draws{\draw#1;}%
\look@for@draw
}
\makeatother
\begin{document}
\begin{tikzpicture}[scale = 4]
\reversedraws
\draw [fill=yellow] (0.445041,0.801937) rectangle (0,1) node[midway] {$1$};
\draw [fill=yellow] (0.692021,0.356896) rectangle (1,1) node[midway] {$2$};
\draw [fill=yellow] (0.692021,0.356895) rectangle (0,0) node[midway] {$3$};
\draw [fill=yellow] (0.137063,0.158832) rectangle (0,1) node[midway] {$4$};
\draw [fill=yellow] (0.384043,0.713791) rectangle (1,0) node[yshift=-5pt,xshift=5pt,midway] {$5$};
\draw [fill=yellow] (0.829084,0.515727) rectangle (0,1) node[midway] {$6$};
\draw [fill=yellow] (0.521105,0.872622) rectangle (0,0) node[midway] {$7$};
\end{tikzpicture}
\end{document}