3

I try to create and order text boxes with tikz-nodes. The nodes are ordered so far using the tikz positioning library. Additionally, I try to have numbers on the background of the nodes by using path picture.

Here is a minimal example:

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\usepackage[outline]{contour}    
\usepackage[english]{babel}
\usepackage{blindtext}    
\usepackage{environ}

\newlength{\myblockwidth}
\newcounter{boxcounter}

\newlength{\mylinewidths}
\newlength{\myinnersep}
\setlength{\mylinewidths}{5pt}
\setlength{\myinnersep}{2ex}

\NewEnviron{mycolorblock}[2][]{%
  \addtocounter{boxcounter}{1}%
  \setlength{\myblockwidth}{#2}%
  \addtolength{\myblockwidth}{-4ex}%
  \contourlength{.333pt}%
  \node[
  #1,
  draw=orange,
  fill=orange!10,
  line width=\mylinewidths,
  inner sep=2ex,
  rounded corners = 2.5ex,
  path picture={%
    \node[white,
    circle, draw=blue!20, line width=\mylinewidths, inner sep=.1pt,
    scale = 15,
    font=\bf] at (path picture bounding box.center)
    {\contour{blue!20}{\the\value{boxcounter}}};}
  ](box\the\value{boxcounter}){
    \begin{minipage}[t][]{\myblockwidth}%
      \BODY
    \end{minipage}%
  };
}
\begin{document}
\begin{tikzpicture}[inner sep=0pt,node distance=5pt]
  \begin{mycolorblock}{\textwidth}
    \blindtext[2]
  \end{mycolorblock}
  \begin{mycolorblock}[below = of box1]{\textwidth}
    \blindtext[2]
  \end{mycolorblock}
\end{tikzpicture}
\end{document}

Process with pdflatex the background number for the second block (and any additional is off-center:
result of example
The background is thereby always shifted away from the center of the first block and the distance from the center of its block seems to be scaled by scale.

I am not even sure it's the positioning library which causes this behavior. If someone could point out how to fix or migrate this issue, I would by grateful.

  • 1
    The node in the path picture is probably inheriting below = of box1. Does putting anchor=center in the path picture node help? – Mark Wibrow May 30 '14 at 10:29

2 Answers2

6

This is not an answer to your question but another way of producing this kind of numbered boxes with tcolorbox.

tcolorbox includes a watermark option to include watermarks into boxes background. You can have text, image and tikz watermarks.

Another option which can simplify a little bit your works is its automatic counter handling. You can create a \newcounter in preamble and option [use counter=yournewcounter] automatically increases the counter which every new tcolorbox. Even more, you don't need to remember which counter is used, because inside tcolorbox declaration the counter value is just \thetcbcounter.

Next is a code example defining boxes similar to your ones.

\documentclass{article}
\usepackage[english]{babel}
\usepackage{blindtext}    
\usepackage[most]{tcolorbox}
\usepackage[outline]{contour}

\newlength{\myblockwidth}
\newlength{\mylinewidths}
\newlength{\myinnersep}
\setlength{\mylinewidths}{5pt}
\setlength{\myinnersep}{2ex}
\contourlength{.333pt}

\newcounter{boxcounter}

\newtcolorbox[use counter=boxcounter]{mycolorblock}[2][]{%
    enhanced,
    notitle,
    colframe=orange,
    colback=orange!10,
    watermark tikz={\node[circle, text=white, draw=blue!20,%
            line width=\mylinewidths, inner sep=.1pt, scale=15,
            font=\bfseries] {\contour{blue!20}{\textcolor{white}{\thetcbcounter}}};},%
      watermark color=white,
      width=#2,
      boxrule=\mylinewidths,
}

\begin{document}

\begin{mycolorblock}{\textwidth}
\thetcbcounter \blindtext[2]
\end{mycolorblock}
\par
\begin{mycolorblock}{\textwidth}
\thetcbcounter \blindtext[2]
\end{mycolorblock}
\end{document}

enter image description here

Update

tcolorbox includes remember as option which assigns a name to the box. This name can be used as a reference in external tikzpictures. Therefore is possible to draw conecting arrows between several tcolorboxes. Next code shows an example.

Now boxes have three parameters. The first one which is optional, will be passed as extra tcolorbox options to mycolorblock. The second one (mandatory) is the watermark number and the third (also mandatory) is the box width.

\documentclass[a4paper]{article}
\usepackage[margin={1cm,2cm,1cm,1cm}]{geometry}
\usepackage[english]{babel}
\usepackage{blindtext}    
\usepackage[most]{tcolorbox}
\usepackage[outline]{contour}
\usepackage{multirow}
\thispagestyle{empty}

\newlength{\myblockwidth}
\newlength{\mylinewidths}
\newlength{\myinnersep}
\setlength{\mylinewidths}{5pt}
\setlength{\myinnersep}{2ex}
\contourlength{.333pt}

\newcounter{boxcounter}

\newtcolorbox{mycolorblock}[3][]{%
    enhanced, 
      nobeforeafter,
    notitle,
    colframe=orange,
    colback=orange!10,
    watermark tikz={\node[circle, text=white, draw=blue!20,%
            line width=\mylinewidths, inner sep=.1pt, scale=15,
            font=\bfseries] {\contour{blue!20}{\textcolor{white}{#2}}};},%
      watermark color=white,
      width=#3,
      boxrule=\mylinewidths,
      remember as ={#2}
}

\begin{document}

\begin{tabular}{cc}
\begin{mycolorblock}{1}{.45\textwidth}
\blindtext[1]
\end{mycolorblock} & 
\begin{mycolorblock}{2}{.45\textwidth}
\blindtext[1]
\end{mycolorblock} \\
\begin{mycolorblock}{3}{.45\textwidth}
\blindtext[1]
\end{mycolorblock}
\end{tabular}

\begin{tikzpicture}[overlay, remember picture, line width=\mylinewidths, draw=orange]
\draw[->] (1.north) --++(90:.75cm) -| (2);
\draw[->] (1.west) --++(180:.75cm) |- (3);
\draw[->] (3) -| (2);
\end{tikzpicture}

\end{document}

enter image description here

Ignasi
  • 136,588
  • That's a nice package. I will use these boxes on a poster to connect them e.g. with arrows. Does the tcolorbox also allow to position boxes relative to each other or would I still need to put them in tikz-nodes for this actions? Example – Hasenblut Jun 02 '14 at 15:05
  • 1
    @Hasenblut. You can use remember as options. It's shown in updated answer. – Ignasi Jun 02 '14 at 19:22
5

The positioning option below=... changes the anchor of the box (the outer node) to north. The path picture node is an inner node and therefore it inherits this anchor.

Adding anchor=center to the options of the node inside the path picture solves the problem:

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning}
\usepackage[outline]{contour}    
\usepackage[english]{babel}
\usepackage{blindtext}    
\usepackage{environ}

\newlength{\myblockwidth}
\newcounter{boxcounter}

\newlength{\mylinewidths}
\newlength{\myinnersep}
\setlength{\mylinewidths}{5pt}
\setlength{\myinnersep}{2ex}

\NewEnviron{mycolorblock}[2][]{%
  \addtocounter{boxcounter}{1}%
  \setlength{\myblockwidth}{#2}%
  \addtolength{\myblockwidth}{-4ex}%
  \contourlength{.333pt}%
  \node[
  #1,
  draw=orange,
  fill=orange!10,
  line width=\mylinewidths,
  inner sep=2ex,
  rounded corners = 2.5ex,
  path picture={%
    \node[white,
    circle, draw=blue!20, line width=\mylinewidths, inner sep=.1pt,
    scale = 15,
    font=\bf,
    anchor=center % set the anchor of the node inside the path picture
    ] at (path picture bounding box.center)
    {\contour{blue!20}{\the\value{boxcounter}}};}
  ](box\the\value{boxcounter}){
    \begin{minipage}{\myblockwidth}%
      \BODY
    \end{minipage}%
  };
}
\begin{document}
\begin{tikzpicture}[inner sep=0pt,node distance=5pt]
  \begin{mycolorblock}{\textwidth}
    \blindtext[2]
  \end{mycolorblock}
  \begin{mycolorblock}[below = of box1]{\textwidth}
    \blindtext[2]
  \end{mycolorblock}
\end{tikzpicture}
\end{document}

enter image description here

esdd
  • 85,675