我正在使用以下内容创建一个小图形。我想缩放尺寸,但 foreach 中绘制的边不会缩放。
\begin{tikzpicture}[scale=0.5] % problem <---
\tikzstyle{every node}=[draw,circle,fill=black,minimum size=4pt,
inner sep=0pt]
\node (a) at (0,0) {};
\node (b) at (0,1) {};
\node (c) at (0,2) {};
\node (d) at (0,3) {};
\node (e) at (1,1) {};
\node (f) at (1,2) {};
\tikz\foreach \x in {0,1,2,3}
\foreach \y in {1,2}
\draw (0, \x) -- (1, \y);
\end{tikzpicture}
问题是,当设置 scale=0.5 时,只有节点会缩小,而不会使用嵌套 foreach 绘制的线条缩小。
我希望得到下图的较小版本(不设置比例)。我该如何实现?我的图表会变得非常大,因此保留使用 foreach 循环的能力非常重要。
答案1
\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.5] % problem <---
\tikzstyle{every node}=[draw,circle,fill=black,minimum size=4pt,
inner sep=0pt]
\node (a) at (0,0) {};
\node (b) at (0,1) {};
\node (c) at (0,2) {};
\node (d) at (0,3) {};
\node (e) at (1,1) {};
\node (f) at (1,2) {};
\foreach \x in {0,1,2,3}
\foreach \y in {1,2}
\draw (0, \x) -- (1, \y);
\end{tikzpicture}
\begin{tikzpicture}[scale=1.5] % problem <---
\tikzstyle{every node}=[draw,circle,fill=black,minimum size=4pt,
inner sep=0pt]
\node (a) at (0,0) {};
\node (b) at (0,1) {};
\node (c) at (0,2) {};
\node (d) at (0,3) {};
\node (e) at (1,1) {};
\node (f) at (1,2) {};
\foreach \x in {0,1,2,3}
\foreach \y in {1,2}
\draw (0, \x) -- (1, \y);
\end{tikzpicture}
\end{document}
答案2
只需更短的代码和正确的语法即可定义节点样式。用于练习。
\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.5]% <--- scaling is not problem
\tikzset{every node/.style = {circle, fill=black, minimum size=4pt, % <---
inner sep=0pt, outer sep=0pt}}
\foreach \i in {0,1,2,3}
\foreach \j in {1,2}
{
\draw[red] (0,\i) node {} -- (1,\j) node {};
}
\end{tikzpicture}
\end{document}
编辑: 在这种情况下,如果希望有命名的节点,只需为节点添加名称。例如:
\documentclass[border=3.141592]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[scale=0.5]% <--- scaling is not problem
\tikzset{
every node/.style = {circle, fill=black, minimum size=4pt,
inner sep=0pt, outer sep=0pt}
}
\foreach \i in {0,1,2,3}
\foreach \j in {1,2}
{
\draw[red] (0,\i) node (a\i) {} -- (1,\j) node (b\j) {};
}
\draw[blue] (a0) -- (a1);
\end{tikzpicture}
\end{document}