5

Is it easier to draw a several box and have words inside the box in latex? I feel Latex looks nicer, rather than I draw in Words document and save as picture and insert picture to latex.

I want to have the box like below:

enter image description here

  • consider providing an MWE? – Raaja Dec 20 '19 at 20:40
  • With tikz, boxes containing text are called node. See the : tex.stackexchange.com/questions/52761/… – AndréC Dec 20 '19 at 20:41
  • 1
    @AndréC. Thanks. I am quite new to Latex, and not sure where I can start from. I able to understand very simple code like \begin{tikzpicture}[stack/.style={ rectangle split, rectangle split parts=5, but not sure how to make a large rectangular box. – aan Dec 20 '19 at 20:44
  • 1
    It is simple to obtain with tabularx, hhline and colortbl. You don't need a hammersledge to crack a nut. – Bernard Dec 20 '19 at 20:46
  • 1
    For starters, forget about style and write simple node. Once you understand the basic mechanism, you can start to see what styles are. – AndréC Dec 20 '19 at 20:49
10

As tikz image, used fit and matrix libraries and the enumitem package:

\documentclass[tikz, margin=5mm]{standalone}
\usetikzlibrary{fit,
                matrix}
\usepackage{enumitem}

\begin{document}
\begin{center}
\setlist[itemize]{nosep,     
                  leftmargin = *         ,
                  before     = \vspace{-0.4\baselineskip},
                     }
    \begin{tikzpicture}[
F/.style args = {#1/#2/#3}{draw, fill=#1, 
                           label={[text width=120mm,
                                   text=#2,
                                   align=center]center:#3},
                           inner sep=0pt, outer sep=0pt,
                           node contents={}
                          },
        font = \sffamily
                        ]
\matrix (m) [matrix of nodes,
             nodes in empty cells,
             nodes={thin, draw,
                    minimum height=4ex, text width=30mm, 
                    inner sep=1mm, outer sep=0pt, anchor=north},
             column sep=2mm,
                row sep=2mm,
                row 3/.style = {nodes={fill=black, text=white}},
                row 4/.style = {nodes={minimum height=5.4\baselineskip}},
                row 5/.style = {row sep=\pgflinewidth,
                                nodes={fill=black, text=white}},
             ]
{   
    &       &       &       \\
    &       &       &       \\
Primary Objectives  
    & Experimental design   
            & Fixed Section/Part    
                    & Testing Variables   \\    
    \begin{itemize}
\item   first item
\item   second item
\item   third item has at least two lines
    \end{itemize}\vfill
    &    \begin{itemize}
    \item   first item
    \item   second item
    \item   third item has at least two lines
    \item   forth item
        \end{itemize}
        &    \begin{itemize}
        \item   first item
        \item   second item
        \item   third item has at least two lines
        \item   forth item
            \end{itemize}
            &    \begin{itemize}
            \item   first item
            \item   second item
            \item   third item has at least two lines
            \item   forth item
                \end{itemize}   \\
    &       &       &           \\
    &       &       &           \\[4mm]
    &       &       &           \\
    &       &       &          \\
};
\node[F=gray!30/black/Study 1,
      fit=(m-1-1) (m-1-4)];
\node[F=white/black/{Aim: Investigate the peformance /
                          and some more text},
      fit=(m-2-1) (m-2-4)];
\node[F=black/white/Hyaphotesis,
      fit=(m-5-1) (m-5-4)];
\node[F=white/black/\begin{itemize}
                \item   H1: Investigate the peformance
                \item   H2: Investigate the peformance
                    \end{itemize},
    minimum height=2\baselineskip,
    fit=(m-6-1) (m-6-4)];
\node[F=gray!30/black/Study 2,
      fit=(m-7-1) (m-7-4)];
\node[F=white/black/{Aim: Investigate the peformance /
                          and some more text},
      fit=(m-8-1) (m-8-4)];
    \end{tikzpicture}
\end{center}
\end{document}

enter image description here

Note: in the case when you like to add more "fit" nodes to your diagram, for example node

 \node[F=gray!30/black/Study 3, fit=(m-9-1) (m-9-4)];

you need first extend matrix with one more row with empty cells.

|improve this answer|||||
  • thanks. Just one question, if I want to add in Study 3, I had tried \node[F=gray!30/black/Study 3, fit=(m-9-1) (m-9-4)];, but seemed not working. Did I miss out something? – aan Dec 21 '19 at 9:57
  • @aan, do you first write ninth row (of empty cells) in matrix? – Zarko Dec 21 '19 at 10:05
  • I just tried row 7/.style = {nodes={fill=black, text=white}},, not sure is this called empty cells? I tried row 9/.style = {nodes={fill=black, text=white}}, – aan Dec 21 '19 at 10:17
  • @ann, no, this just format row 7. You new write additional row in the matrix. See edited answer. – Zarko Dec 21 '19 at 10:28
  • 1
    Very very very nice. I appreciated lot also your work. – Sebastiano Dec 21 '19 at 11:57
6

The following based on tabularx might serve as a starting point:

enter image description here

\documentclass{article}
\usepackage{tabularx}
\usepackage[table]{xcolor}
\usepackage{calc}
\usepackage{hhline}

\begin{document}
\noindent
\begin{tabularx}{\textwidth}{|*{3}{>{\raggedright\arraybackslash}X|c|}X|}
\hline
\multicolumn{7}{|>{\centering\arraybackslash}p{\textwidth-2\tabcolsep-2\arrayrulewidth}|}{\cellcolor{gray}Study 1}\\
\hline
\multicolumn{7}{c}{}\\
\hline
\multicolumn{7}{|>{\centering\arraybackslash}p{\textwidth-2\tabcolsep-2\arrayrulewidth}|}{Aim: some text here}\\
\hline
\multicolumn{7}{c}{}\\
\hhline{-~-~-~-}
\cellcolor{black}\textcolor{white}{Primary Objectives} && \cellcolor{black}\textcolor{white}{Experimental Design} && \cellcolor{black}\textcolor{white} {Fixed Section/ Part} && \cellcolor{black}\textcolor{white} {Testing Variables}\\
\hhline{-~-~-~-}
\multicolumn{7}{c}{}\\
\hhline{-~-~-~-}
 some text here &&  some other text here &&  more in this cell &&  additional text here\\
 \hhline{-~-~-~-}
 \multicolumn{7}{c}{}\\
 \hline
\multicolumn{7}{|>{\centering\arraybackslash}p{\textwidth-2\tabcolsep-2\arrayrulewidth}|}{\cellcolor{black}\textcolor{white}{Hypothesis}}\\
\hline
\end{tabularx}
\end{document}
|improve this answer|||||
  • thanks. Didnt know can draw using a table. I have one question for \usepackage{calc}, it said Latex Error: Option clash with package xcolour. – aan Dec 20 '19 at 21:49
  • @aan: The calc package will most likely not be the cause for the quoted error message. Did you load the xcolor package twice and with different options? – leandriis Dec 20 '19 at 21:51
5

I think that leandriis answer is the way to go, but if you want to use TikZ, use a matrix. You can then use the aux file to synchronize the relevant dimensions.

\documentclass{article}
\usepackage{geometry}
\usepackage{tikz}
\usetikzlibrary{matrix,calc,positioning}
\makeatletter
\newcommand{\WriteAux}[1]{%
\immediate\write\@mainaux{\xdef\string#1{#1}\relax}}
\makeatother
\pgfkeys{initialize dimension/.code={\unless\ifdefined#1
 \def#1{0pt}
 \fi}}
\begin{document}
\begin{table}
\centering
 \begin{tikzpicture}[font=\sffamily,node distance=1ex]
  \pgfkeys{initialize dimension/.list={\myheightA,\myheightB,\mywidth}}
  \node[draw,text width=\mywidth,align=center,fill=gray](Study){Study 1};
  \node[below=of Study,draw,text width=\mywidth,align=center](aim){Aim: Investigate the
  performance\,/\,Investigate Investigate Investigate Investigate};
  \node[below=of aim,matrix of nodes,nodes={text width=0.2\textwidth,align=left,
      fill,text=white,minimum height=\myheightA,inner sep=2pt,anchor=north},
    column sep=1ex,inner sep=0pt] (matA) {
    {Primary Objectives} &
    {Experimental design} &
    {Fixed Section/Part} &
    {Primary Objectives} \\
  };
  \node[below=of matA,matrix of nodes,nodes={text width=0.2\textwidth,align=left,
      draw,minimum height=\myheightB,inner sep=2pt,anchor=north},
    column sep=1ex,inner sep=0pt] (matB) {
  {\vspace*{-\baselineskip}\begin{itemize}
  \item bla
  \item blub
  \end{itemize}} &
  {\vspace*{-\baselineskip}\begin{itemize}
  \item bla
  \item blub
  \end{itemize}} &
  {\vspace*{-\baselineskip}\begin{itemize}
  \item bla
  \item blub
  \item pft
  \end{itemize}} &
  {\vspace*{-\baselineskip}\begin{itemize}
  \item bla
  \item blub
  \end{itemize}} \\
  };
  \node[below=of matB,text width=\mywidth,fill,text=white,draw,align=center] (Hypo)
   {Hypotheses};
  \node[below=0pt of Hypo,text width=\mywidth,align=left,draw] (Pft)
   {\vspace*{-\baselineskip}\begin{itemize}
  \item bla
  \item blub
  \end{itemize}};
  \node[below=1.5em of Pft,draw,text width=\mywidth,align=center,fill=gray](Study2){Study 2};
  \node[below=of Study2,draw,text width=\mywidth,align=center](aim2){Aim: Investigate the
  performance\,/\,Investigate Investigate Investigate Investigate};
  % book keeping:
  \path let \p1=($(matB-1-1.north)-(matB-1-1.south)$),
      \p2=($(matB-1-2.north)-(matB-1-2.south)$),
      \p3=($(matB-1-3.north)-(matB-1-3.south)$),
      \p4=($(matB-1-4.north)-(matB-1-4.south)$),
      \n1={max(\y1,\y2,\y3,\y4)},
      \p5=($(matB.east)-(matB.west)$),
      \p6=($(matA-1-1.north)-(matA-1-1.south)$),
      \p7=($(matA-1-2.north)-(matA-1-2.south)$),
      \p8=($(matA-1-3.north)-(matA-1-3.south)$),
      \p9=($(matA-1-4.north)-(matA-1-4.south)$),
      \n2={max(\y6,\y7,\y8,\y9)}     in
  \pgfextra{\pgfmathsetmacro{\mywidth}{\x5-8pt}\edef\mywidth{\mywidth pt}\edef\myheightA{\n2}\edef\myheightB{\n1}%
      \WriteAux{\mywidth}\WriteAux{\myheightA}\WriteAux{\myheightB}};
 \end{tikzpicture}
 \caption{Some table.}
\end{table}
\end{document}

enter image description here

|improve this answer|||||
  • I should have maybe stressed that in this answer you everything will adjust if you decide to make, say, some node above the matrix taller.... – Schrödinger's cat Dec 21 '19 at 6:11
4

With TikZ, a box that contains text is called a node. To start, let's forget about style, once you understand the basic syntax, you can use them.

1. Here is a simple example without any style.

I use the positioning library which allows to place the node in relation to each other. I proceeded by trial and error to find the width of the top one.

screenshot

\documentclass[border=5mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node[draw,fill=black,text=white] (primary) at (0,0) {Primary objectives};
\node[draw,fill=black,text=white](experimental)[right=of primary]  {Experimental Design};
\node[draw,fill=black,text=white](section)[right=of experimental]  {Fixed section / Part};
\node[draw,fill=black,text=white](testing)[right=of section]  {Testing variables};
\node[draw](aim)[above=of primary.west,anchor=west,minimum width=15.8cm]{Aim: investigate the performance / Investigate  Investigate Investigate Investigate};
\end{tikzpicture}

\end{document}

2. More precise relative placements:

It is possible to fix a minimum width to the box. This is appropriate here because the text is smaller than the box. I fixed it at 3.5cm. The distance between the nodes can be set globally with the key node distance=2mm.

Likewise, by setting a minimum height greater than each text, here minimum height=2em the boxes all have the same height. At each node, TikZ adds an invisible outer margin which by default measures half of the line width, here 0.4 pt. By setting this value to outer sep=0pt, the top box is as wide as the four boxes below it.

So the width of the top box is 4x3.5cm + 3x0.2cm=14.6cm.

\begin{tikzpicture}[node distance=2mm,minimum width=3.5cm,minimum height=2em,outer sep=0pt]

This key node distance=2mm only works if the distance between two nodes is not set locally. For example, I placed the top node 1mm above the other nodes. And so the global key node distance=2mm is not active.

I position this box above its north west corner and not above its west edge to avoid overlapping boxes.

\node[draw](aim)[above=1mm of primary.north west,anchor=south west,minimum width=14.6cm]{Aim: investigate the performance / Investigate Investigate Investigate Investigate Investigate}; and so the node distance=2mm option is ignored.

Translated with www.DeepL.com/Translator (free version)

screenshot

\documentclass[border=5mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[node distance=2mm,minimum width=3.5cm,minimum height=2em,outer sep=0pt]
\node[draw,fill=black,text=white] (primary) at (0,0) {Primary objectives};
\node[draw,fill=black,text=white](experimental)[right=of primary]  {Experimental Design};
\node[draw,fill=black,text=white](section)[right=of experimental]  {Fixed section / Part};
\node[draw,fill=black,text=white](testing)[right=of section]  {Testing variables};
\node[draw](aim)[above=1mm of primary.north west,anchor=south west,minimum width=14.6cm]{Aim: investigate the performance / Investigate  Investigate Investigate Investigate};
\end{tikzpicture}

\end{document}
|improve this answer|||||
  • thanks. I can easily understand your code. Thanks. Between, how to make the box larger in size? I want to have box 1 and box 2 close and large enough. – aan Dec 20 '19 at 21:46
  • @aan That's it, I've added some details about the relative placement of the nodes. – AndréC Dec 21 '19 at 6:04
  • @aan I just added complements on the outer margin outer sep=0pt and the minimum height of each box minimum height=2em. – AndréC Dec 22 '19 at 10:21
3

For such clear structures, I think it's easier to use a tcbraster or tcbitemize from tcolorbox. Here you have a starting point example:

\documentclass[a4paper]{article}
\usepackage[most]{tcolorbox}

\tcbset{
    sharp corners,
    fontupper=\sffamily,
    halign=center,
    study/.style={colback=black!30},
    aim/.style={colback=white},
    text/.style={colback=white, halign=left},
    name/.style={colback=black, colupper=white}
    }
\begin{document}
\begin{tcbitemize}[raster columns=4]
\tcbitem[study, raster multicolumn=4] Study 1
\tcbitem[aim, raster multicolumn=4] Aim: Investigate the performance/Investigate Investigate
\tcbitem[name] Primary Objectyives
\tcbitem[name] Experimental Design
\tcbitem[name] Fixed Section/Part
\tcbitem[name] Testing Variables
\tcbitem[text, halign=left] - Investigate\\ - System Investigate\\ - Investigate
\tcbitem[text] - Investigate\\ - System Investigate\\ - Investigate
\tcbitem[text] - Investigate\\ - System Investigate\\ - Investigate
\tcbitem[text] - Investigate\\ - System Investigate\\ - Investigate
\tcbitem[name, raster multicolumn=4] Hypotheses
\tcbitem[text, raster multicolumn=4] \begin{itemize}
\item H1: Investigate the performance/Investigate
\item H2: Investigate the performance/Investigate
\end{itemize}
\tcbitem[study, raster multicolumn=4] Study 2
\tcbitem[aim, raster multicolumn=4] Aim: Investigate the performance/Investigate Investigate
\end{tcbitemize}
\end{document}

enter image description here

|improve this answer|||||
  • I vote up also your nice answer. – Sebastiano Dec 22 '19 at 21:50
  • @lgnasi, thanks. Is tcbset conflict with \usepackage{hhline, boldline}? Can I have both in overleaf? It seemed cannot – aan Dec 23 '19 at 2:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.