我正在尝试为井字游戏绘制一棵树。树的输出会相当大,我希望页面能够随着树的生长以适当的间距自动缩放树的大小。这可能吗?
\documentclass{article}
\usepackage{qtree}
\usepackage{tikz}
\newcounter{num}
\newcommand{\tictactoe}[1]
{
\begin{tikzpicture}[line width=2pt]
\def\r{3mm}
\tikzset{
circ/.pic={\draw circle (\r);},
cross/.pic={\draw (-\r,-\r) -- (\r,\r) (-\r,\r) -- (\r,-\r);},
opt/.pic={\draw[opacity=0.2] (-\r,-\r) -- (\r,\r) (-\r,\r) -- (\r,-\r);}
}
% The grid
\foreach \i in {1,2} \draw (\i,0) -- (\i,3) (0,\i) -- (3,\i);
% Numbering the cells
\setcounter{num}{0}
\foreach \y in {0,...,2}
\foreach \x in {0,...,2}
{
\coordinate (\thenum) at (\x+0.5,2-\y+0.5);
%\node[opacity=0.5] at (\thenum) {\sffamily\thenum}; % Uncomment to see numbers in the cells
\addtocounter{num}{1}
}
\def\X{X} \def\x{x} \def\O{O} \def\n{n}
\foreach \l [count = \i from 0] in {#1}
{
\if\l\X \path (\i) pic{cross};
\else
\if\l\O \path (\i) pic{circ};
\else
\if\l\x \path (\i) pic{opt};
\else
\if\l\n \node[opacity=0.5] at (\i) {\sffamily\i};
\fi
\fi
\fi
\fi
}
\end{tikzpicture}
}
\begin{document}
\Tree [.\tictactoe{X,0,0,
0,0,0,
0,0,0} [.\tictactoe{X,0,0,
O,0,0,
0,0,0} {\tictactoe{X,0,0,O,0,0,X,0,0}} \tictactoe{X,0,0,O,0,0,0,X,0} \tictactoe{X,0,0,O,0,0,0,0,X} \tictactoe{X,0,0,O,X,0,0,0,0} ]
[.\tictactoe{X,0,0,
0,0,0,
0,O,0}
]
[.\tictactoe{X,0,0,
0,O,0,
0,0,0}
]
[.\tictactoe{X,0,0,
0,0,0,
O,0,0}
]
[.\tictactoe{X,0,0,
0,0,0,
0,0,O}
]
]
\end{document}
井字游戏板的 tikz 来自这里: https://tex.stackexchange.com/a/634705/251522
答案1
- 而不是
qtree
使用forest
包 - 画的是“tictactoe”的小符号
- 对于“tictactoe”使用
tabularray
表格 - 图像未缩放
- 使用该
geometry
包可以扩大文本区域
(红线表示页面布局)
\documentclass{article}
\usepackage{geometry}
%--------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
\usepackage{pdflscape}
\usepackage{forest}
\usepackage{tabularray}
\newcommand{\C}{\tikz[baseline=-0.75mm]{\draw[thick] (0,0) circle[radius=1.5mm];}}
\newcommand{\T}{\tikz[baseline=-0.75mm]{\draw[thick] (-1.5mm,-1.5mm) -- (1.5mm, 1.5mm)
(-1.5mm, 1.5mm) -- (1.5mm,-1.5mm);}
}
\newcommand{\tictactoe}[1]
{
\begin{tblr}{hline{2,3} = 0.8pt, vline{2,3} = 0.8pt,
colspec = {@{} *{3}{Q[c,m,wd=3.3mm, mode=math]} @{}},
colsep=3pt,
}
#1
\end{tblr}
}
\begin{document}
\begin{figure}
\centering
\begin{forest}
for tree = {l sep=12mm,
s sep=6mm,
if level>= 1{s sep=1mm }{},
parent anchor = south,
child anchor = north,
}
[\tictactoe{\T & & \\
& & \\
& & }
[\tictactoe{\T & & \\
\C & & \\
& & }
[\tictactoe{\T & & \\
\C & & \\
\T & & }]
[\tictactoe{\T & & \\
\C & & \\
& \T & }]
[\tictactoe{\T & & \\
\C & & \\
& & \T}]
[\tictactoe{\T & & \\
\C & \T & \\
& & }]
]
[\tictactoe{\T & & \\
& & \\
& \C & }]
[\tictactoe{\T & & \\
& \C & \\
& & }]
[\tictactoe{\T & & \\
& & \\
\C & & }]
[\tictactoe{\T & & \\
& & \\
& & \C}]
]
\end{forest}
\end{figure}
\end{document}
附录:
如果您希望保留由article
文档类默认定义的页面布局,那么您需要稍微减少井字游戏符号及其之间的距离:
\documentclass{article}
%--------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
\usepackage{pdflscape}
\usepackage{forest}
\usepackage{tabularray}
\newcommand{\C}{\tikz[baseline=-0.75mm]{\draw[thick] (0,0) circle[radius=1mm];}} % <---
\newcommand{\T}{\tikz[baseline=-0.75mm]{\draw[thick] (-1mm,-1mm) -- (1mm, 1mm) % <---
(-1mm, 1mm) -- (1mm,-1mm);} % <---
}
\newcommand{\tictactoe}[1]%
{%
\begin{tblr}{hline{2,3} = 0.8pt, vline{2,3} = 0.8pt,
colspec = {@{} *{3}{Q[c,m,wd=2mm, mode=math]} @{}}, % <---
colsep=3pt,
}
#1
\end{tblr}
}
\begin{document}
\begin{figure}
\centering
\begin{forest}
for tree = {l sep=12mm,
s sep= 5mm, % <---
if level>= 1{s sep=1mm }{},
parent anchor = south,
child anchor = north,
}
[\tictactoe{\T & & \\
& & \\
& & }
[\tictactoe{\T & & \\
\C & & \\
& & }
[\tictactoe{\T & & \\
\C & & \\
\T & & }]
[\tictactoe{\T & & \\
\C & & \\
& \T & }]
[\tictactoe{\T & & \\
\C & & \\
& & \T}]
[\tictactoe{\T & & \\
\C & \T & \\
& & }]
]
[\tictactoe{\T & & \\
& & \\
& \C & }]
[\tictactoe{\T & & \\
& \C & \\
& & }]
[\tictactoe{\T & & \\
& & \\
\C & & }]
[\tictactoe{\T & & \\
& & \\
& & \C}]
]
\end{forest}
\end{figure}
\end{document}
经过这些修改后,代码中以 标记% <---
,结果仍然易于阅读:
答案2
使用调整框(自动调整图形以横跨页面\textwidth
)并稍微修改\tictactoe
命令,您就可以实现想要的效果。如果您不喜欢这种方法,那么您可以将生成的图形旋转 90 度以横跨页面的垂直空间。
\documentclass{article}
\usepackage{showframe} % added for page layout
\usepackage{qtree}
\usepackage{tikz}
\usepackage{adjustbox} % added and needed!
\newcounter{num}
\newcommand{\tictactoe}[1]
{
\begin{tikzpicture}[scale=0.25, every node/.style={scale=0.25}, line width=0.75pt]
\def\r{2.5mm}
\tikzset{
circ/.pic={\draw circle (\r);},
cross/.pic={\draw (-\r,-\r) -- (\r,\r) (-\r,\r) -- (\r,-\r);},
opt/.pic={\draw[opacity=0.2] (-\r,-\r) -- (\r,\r) (-\r,\r) -- (\r,-\r);}
}
% The grid
\foreach \i in {1,2} \draw (\i,0) -- (\i,3) (0,\i) -- (3,\i);
% Numbering the cells
\setcounter{num}{0}
\foreach \y in {0,...,2}
\foreach \x in {0,...,2}
{
\coordinate (\thenum) at (\x+0.5,2-\y+0.5);
%\node[opacity=0.5] at (\thenum) {\sffamily\thenum}; % Uncomment to see numbers in the cells
\addtocounter{num}{1}
}
\def\X{X} \def\x{x} \def\O{O} \def\n{n}
\foreach \l [count = \i from 0] in {#1}
{
\if\l\X \path (\i) pic{cross};
\else
\if\l\O \path (\i) pic{circ};
\else
\if\l\x \path (\i) pic{opt};
\else
\if\l\n \node[opacity=0.5] at (\i) {\sffamily\i};
\fi
\fi
\fi
\fi
}
\end{tikzpicture}
}
\begin{document}
\begin{adjustbox}{width=\linewidth}
\Tree [.\tictactoe{X,0,0,
0,0,0,
0,0,0} [.\tictactoe{X,0,0,
O,0,0,
0,0,0} {\tictactoe{X,0,0,O,0,0,X,0,0}} \tictactoe{X,0,0,O,0,0,0,X,0} \tictactoe{X,0,0,O,0,0,0,0,X} \tictactoe{X,0,0,O,X,0,0,0,0} ]
[.\tictactoe{X,0,0,
0,0,0,
0,O,0}
]
[.\tictactoe{X,0,0,
0,O,0,
0,0,0}
]
[.\tictactoe{X,0,0,
0,0,0,
O,0,0}
]
[.\tictactoe{X,0,0,
0,0,0,
0,0,O}
]
]
\end{adjustbox}
\end{document}