3

I'm trying to accomplish drawing a tree for a tic-tac-toe game. The output of the tree will be rather large, and I would like the page to auto scale the size of the tree as it grows with appropriate spacing. Is that possible?

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

enter image description here

the tikz for the tic-tac-toe board is from here: https://tex.stackexchange.com/a/634705/251522

Ziggy
  • 416

2 Answers2

5
  • instead of qtree is used forest package
  • drawn are smaller symbols for "tictactoe"
  • for "tictactoe" is used tabularray table
  • image is not scaled
  • text area is enlarged by use of the geometry package

enter image description here

(red lines indicate page layout)

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

Addendum: in the case, that you like to preserve page layout is is default defined by article document class, the you need a wee bit reduce tictactoe symbols and distance between them:

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

After this changes,in code are marked by % <---, result is still easy readable:

enter image description here

Zarko
  • 296,517
  • Very nice suggestion without scaling... – MadyYuvi Jul 02 '22 at 03:34
  • How does geometry control the scaling? – Ziggy Jul 02 '22 at 21:48
  • @MaxBudnick, package geometry is intended for defining of pages layout. It has nothing with scaling (any kind). In my solution us by it enlarge text area on page. This indicates red lines in showed image of tree- – Zarko Jul 02 '22 at 21:56
1

With the usage of adjustbox (autofits the figure to span the \textwidth) and slightly modifying the \tictactoe command, you can achieve what you want. In case you don't favour this approach, then you might rotate the figure generated 90 degrees to span the vertical space of the page.

Output

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

M. Al Jumaily
  • 4,035
  • 2
  • 11
  • 26
  • This is a good solution. It's easier for me to understand because it's less modified. I need to continue to expand my tree. The tree syntax is very confusing. I think I need to work on understanding the syntax a bit more. – Ziggy Jul 02 '22 at 21:49
  • @MaxBudnick, avoid scaling as much as possible. Using it you will lost consistency of font sizes in document. – Zarko Jul 03 '22 at 04:46