[Tex/LaTex] Are three boxes inside a box possible

boxescolumnsnesting

I am making a yes or no question inside a box and I have difficulty on creating the command for it. Would you mind to help me make something that will output this:

enter image description here

Best Answer

As I said in Define a command (possibly recursively) to wrap text in a command n times, when it comes to drawing boxes, tikz is the way to go.

Here is a macro version that accepts four mandatory parameters for each of the text elements. So with

\BoxedQuestion{Is this what you are looking for?}{YES}{NO}{NONE}

you get your plain vanilla box:

enter image description here

However, it also accepts two optional parameters that allow you to style the outer and inner boxes. This illustrates just a few of the various options you can tweak where you can adjust alignment, draw and fill colors:

enter image description here

Box Width:

To control the width of the box, you can add the text width=<length> option:

enter image description here

Horizontal Spacing between Boxes:

A macro has been defined to specify the horizontal spacing. It is currently defined to \hfill which will even space out the three boxes. You can adjust this if you do prefer them separated by a fixed amount of space. So with this defined as

\newcommand*{\HorizontalSpacing}{\hspace*{2.0cm}}

you get:

enter image description here

Note that a fixed distance for horizontal separation requires some care as illustrated by the 4th box above.

Also, this necessitated a slightly different use mode in terms of how the alignment of the first line is controlled: Instead of the tikz options I used a \fill on the appropriate side.


Code: Boxes Evenly Distributed

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}

\newcommand*{\HorizontalSpacing}{\hfill}% equally spaced boxes
%\newcommand*{\HorizontalSpacing}{\hspace*{2.0cm}}% fixed space boxes

\newlength{\InnerSep}
\setlength{\InnerSep}{0.1cm}

\newcommand{\Boxed}[2][]{%
    % #1 = box draw/fill options
    % #2 = text
    \begin{tikzpicture}[baseline, inner sep=2pt, outer sep=0]
            \node [#1] (Origin) {#2};
            \draw [thick, draw=black, #1] 
                (Origin.south west) rectangle (Origin.north east) ;
    \end{tikzpicture}%
}


\tikzset{Outer Box Style/.style={align=center}}
\tikzset{Inner Box Style/.style={
    inner sep=2pt, yshift=0.7ex, 
    text width=1ex, minimum height=2ex
    }
}

\newlength{\BoxWidth}
\NewDocumentCommand{\BoxedQuestion}{O{} O{} m m m m}{
    % #1 = optional draw options for outer box
    % #2 = optional draw options for inner boxes
    % #3 = text of question
    % #4 = choice 1
    % #5 = choice 2
    % #6 = choice 3
    \setlength{\BoxWidth}{\dimexpr\linewidth-2\InnerSep-2\pgflinewidth\relax}%
    \par\noindent\Boxed[Outer Box Style, text width=\BoxWidth, #1]{%
        #3\strut%
        \par\bigskip\noindent%
        \hspace*{\InnerSep}\Boxed[Inner Box Style, #2]{}\,#4%
        \HorizontalSpacing\Boxed[Inner Box Style, #2]{}\,#5%
        \HorizontalSpacing\Boxed[Inner Box Style, #2]{}\,#6%
        \hspace*{\InnerSep}\null%
    }%
}%


\begin{document}
\BoxedQuestion
    {Is this what you are looking for?}
    {YES}{NO}{NONE}

\medskip
\BoxedQuestion[draw=red, align=left][draw=blue, fill=yellow!50]
    {Is this what you are looking for?}
    {YES}{NO}{NONE}

\medskip
\BoxedQuestion
    [draw=red, ultra thick, loosely dotted, align=right, 
        fill=green!30, fill opacity=0.2, text opacity=1]
    [solid, ultra thick, draw=blue, fill=orange!50]
    {Is this what you are looking for?}
    {YES}{NO}{NONE}


\medskip
\BoxedQuestion[text width=6cm]
    [solid, ultra thick, draw=blue, fill=orange!50]
    {Is this what you are looking for?}
    {YES}{NO}{NONE}

\BoxedQuestion[text width=8cm]
    [solid, ultra thick, draw=blue, fill=orange!50]
    {Is this what you are looking for?}
    {YES}{NO}{NONE}
\end{document}

Code: Fixed Horizontal Spacing

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}

%\newcommand*{\HorizontalSpacing}{\hfill}% equally spaced boxes
\newcommand*{\HorizontalSpacing}{\hspace*{2.0cm}}% fixed space boxes

\newlength{\InnerSep}
\setlength{\InnerSep}{0.1cm}

\newcommand{\Boxed}[2][]{%
    % #1 = box draw/fill options
    % #2 = text
    \begin{tikzpicture}[baseline, inner sep=2pt, outer sep=0]
            \node [#1] (Origin) {#2};
            \draw [thick, draw=black, #1] 
                (Origin.south west) rectangle (Origin.north east) ;
    \end{tikzpicture}%
}


\tikzset{Outer Box Style/.style={align=center}}
\tikzset{Inner Box Style/.style={
    inner sep=2pt, yshift=0.7ex, 
    text width=1ex, minimum height=2ex
    }
}

\newlength{\BoxWidth}
\NewDocumentCommand{\BoxedQuestion}{O{} O{} m m m m}{
    % #1 = optional draw options for outer box
    % #2 = optional draw options for inner boxes
    % #3 = text of question
    % #4 = choice 1
    % #5 = choice 2
    % #6 = choice 3
    \setlength{\BoxWidth}{\dimexpr\linewidth-2\InnerSep-2\pgflinewidth\relax}%
    \par\noindent\Boxed[Outer Box Style, text width=\BoxWidth, #1]{%
        #3\strut%
        \par\bigskip\noindent%
        \hspace*{\InnerSep}\Boxed[Inner Box Style, #2]{}\,#4%
        \HorizontalSpacing\Boxed[Inner Box Style, #2]{}\,#5%
        \HorizontalSpacing\Boxed[Inner Box Style, #2]{}\,#6%
        \hspace*{\InnerSep}%
    }%
}%


\begin{document}
\BoxedQuestion
    {Is this what you are looking for?}
    {YES}{NO}{NONE}

\medskip
\BoxedQuestion[draw=red][draw=blue, fill=yellow!50]
    {Is this what you are looking for?\hfill }
    {YES}{NO}{NONE}

\medskip
\BoxedQuestion
    [draw=red, ultra thick, loosely dotted, 
        fill=green!30, fill opacity=0.2, text opacity=1]
    [solid, ultra thick, draw=blue, fill=orange!50]
    {\hfill Is this what you are looking for?}
    {YES}{NO}{NONE}


\medskip
\BoxedQuestion[text width=6cm]
    [solid, ultra thick, draw=blue, fill=orange!50]
    {Is this what you are looking for?}
    {YES}{NO}{NONE}

\BoxedQuestion[text width=8cm]
    [solid, ultra thick, draw=blue, fill=orange!50]
    {Is this what you are looking for?}
    {YES}{NO}{NONE}
\end{document}
Related Question