5

I'm pretty new to latex, and what I am trying to create a simple treemap, by simple I mean really simple.

I have managed to produce a simple box at a given position and width, but I'm not too sure how to create a border around the textblock.

\begin{textblock}{200}(400,200)
  \centering
  \textblockcolour{red}
  \vspace{20mm}
    { \bfseries \Large Hello World }
  \vspace{20mm}
\end{textblock}

My Example usage is below:

\documentclass{article}

\usepackage[a4paper]{geometry}
\usepackage[absolute,overlay]{textpos}
  \setlength{\TPHorizModule}{1mm}
  \setlength{\TPVertModule}{1mm}
  \setlength{\parindent}{0mm}
\usepackage{color}


\begin{document}

  \begin{textblock}{200}(400,200)
    \centering
    \textblockcolour{red}
    \vspace{20mm}
      { \bfseries \Large Hello World }
    \vspace{20mm}
  \end{textblock}

\end{document}

Any help would be appreciated.

lockstep
  • 250,273
clangers
  • 153
  • The textblock documentation suggests doing this using tcolorbox, and gives a link to this question: https://tex.stackexchange.com/questions/34088/how-to-draw-frame-with-rounded-corners-around-box –  Jul 13 '21 at 13:29

1 Answers1

6

You can use the showboxes package option (in the example below I changed the settings for the textblock since the original settings place the block outside the page):

\documentclass{article}
\usepackage[absolute,overlay,showboxes]{textpos}
\usepackage{xcolor}

\setlength{\TPHorizModule}{1mm}
\setlength{\TPVertModule}{1mm}
\setlength{\parindent}{0mm}

\begin{document}

\begin{textblock}{200}(0,200)
  \centering
  \textblockcolour{red}
  \vspace{20mm}
    { \bfseries \Large Hello World }
  \vspace{20mm}
\end{textblock}

\end{document}

enter image description here

However, to draw your treemap I would suggest you to use PGF/TikZ instead.

Here's a simple possibility, using TikZ, to draw a treemap:

\documentclass{article}
\usepackage{tikz}

\definecolor{green1}{RGB}{221,242,151}
\definecolor{green2}{RGB}{161,221,135}
\definecolor{purple1}{RGB}{149,87,164}
\definecolor{blue1}{RGB}{42,144,158}

\begin{document}

\begin{tikzpicture}

\draw[draw=black,fill=green1] 
  (0,0) rectangle (3.5,-6);
\draw[draw=black,fill=purple1] 
  (3.5,0) rectangle (6,-6);
\draw[draw=black,fill=green2] 
  (0,-6) rectangle (3.8,-9);
\draw[draw=black,fill=blue1] 
  (3.8,-6) rectangle (6,-8);
\draw[draw=black,fill=purple1] 
  (3.8,-8) rectangle (6,-9);

\node at (1.75,-3) {Paul};
\node at (4.75,-3) {Dennis};
\node at (1.9,-7.5) {Rick};
\node at (4.9,-7) {Simon};
\node at (4.9,-8.5) {Alvin};
\end{tikzpicture}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
  • Thanks for that! I did look at the PGF/Tikz package, but I found it quite hard to understand (being a new latex user). I did manage to find an example to produce something similar \begin{tikzpicture} \draw (0,0) rectangle (2,2); \draw (0,0) node [fill=red] {Text at \verb!node 1!}; \end{tikzpicture} However I can't seem to position it where I want (I'm using a mm grid). I also couldn't work out how to add padding to the text. – clangers Dec 12 '12 at 17:54
  • @clangers I updated my answer showing a possibility using TikZ. – Gonzalo Medina Dec 12 '12 at 18:21
  • @clangers give me some minutes and I'll add a more intuitive TikZ-based solution. – Gonzalo Medina Dec 12 '12 at 18:34
  • @clangers I updated my answer with a simpler TikZ-based solution. – Gonzalo Medina Dec 12 '12 at 18:44
  • thanks for this, it is exactly what I was originally trying to produce. A quick question though, I noticed that your example uses pt for positioning (and sizes?). How can I use mm instead, as I currently have a mm grid on my document. So for example \draw[draw=black,fill=green1] (40mm,100mm) rectangle (60mm,50mm) doesn't seem to place the top-left of the square at 40x100. – clangers Dec 13 '12 at 10:58
  • @clangers Since I didn't specify the unit, my code uses centimeters (the default unit in TikZ); of course you can use millimeters instead. Here's a simple example with a grid: – Gonzalo Medina Dec 13 '12 at 14:41
  • @clangers `\documentclass{article} \usepackage{tikz}

    \definecolor{green1}{RGB}{221,242,151} \definecolor{green2}{RGB}{161,221,135} \definecolor{purple1}{RGB}{149,87,164} \definecolor{blue1}{RGB}{42,144,158}

    \begin{document}

    \begin{tikzpicture} \draw[step=1mm,ultra thin,gray!20] (-5,-5) grid (7,10); \draw[step=1cm,ultra thin,gray!70] (-5,-5) grid (7,10); \foreach \x in {-5,...,7} \node[label = above:$\x$] at (\x,10) {}; \foreach \y in {-5,...,10} \node[label = right:$\y$] at (7,\y) {}; \draw[draw=black,fill=green1] (40mm,100mm) rectangle (60mm,50mm); \end{tikzpicture} \end{document}`

    – Gonzalo Medina Dec 13 '12 at 14:41