1

I was trying to reproduce some tcolorbox– boxes. Now consider the following MWE:

\documentclass[12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[table]{xcolor}
\usepackage{tikz,lipsum,libertine,microtype,colortbl,tcolorbox,amsmath,amssymb,amsfonts}
\usetikzlibrary{backgrounds}
\usepackage[explicit]{titlesec}
\newcommand{\changefont}[3]{\fontfamily{#1}\fontseries{#2}\fontshape{#3}\selectfont}

\everymath{\displaystyle}

%\titleformat{\chapter}[display]
%{\normalfont\huge\bfseries}{\filcenter\underline{\MakeUppercase{\textls[400]{\chaptertitlename}}\ \thechapter}}{20pt}{\Huge}

%\titleformat{\chapter}[display]
%{\normalfont\huge\bfseries}{\fcolorbox{red}{orange}{#1}\thechapter\hspace{1.5ex}\chaptertitlename}{20pt}{\Huge}

%\titleformat{\chapter}[display]
%{\normalfont\huge\bfseries}{
%   \begin{tcolorbox}[colframe=blue!20,colback=blue!20,skin=widget,height=3cm]
%       \vspace{2cm}
%       \hfill\color{red}#1
%   \end{tcolorbox}
%}{-10pt}{\Huge}

\titleformat{\chapter}[display]
{\normalfont\huge\bfseries}{
    \begin{tikzpicture}
        \node[fill=blue!20,minimum width=\textwidth,minimum height=2cm] (a) {};
            \node[above left] at (a.south east) {\bf\textsf{#1}};
            \node[gray] at (a.north east) {\scalebox{2}{\changefont{pnc}{m}{sl}\thechapter}};
    \end{tikzpicture}
}{-40pt}{\Huge}
\begin{document}

    \chapter{Einleitung}
    \lipsum[1]\par
    Hallo, hier steht ein Blindtext. \marginpar{\it Tipp: Versuch es selber zu lösen, dadurch lernst du mehr.} Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.
        \vspace{1cm}
    \begin{table}[h!]
    \centering
            \rowcolors{1}{blue!20}{blue!10}
        \begin{tabular}{>{\sf}r >{\sf}l}
            \rowcolor{blue!30} 
            \normalfont\textbf{Meine Arbeit} & \normalfont\textbf{Deine Arbeit} \\
            Einkauf & heute \\
            Zeichnung & morgen \\
            Arbeit & gestern \\
            Hier und da & Jetzt da und dort
        \end{tabular}
    \caption{Beispiel einer Einkaufsliste}
    \end{table}
        \thispagestyle{empty}\newpage
    \lipsum[1] Hier ist es wichtig \marginpar{\it Mit der Zeit tut man sich leichter \ldots} \lipsum[1] 

    \vspace{1ex}\noindent\begin{tikzpicture}
        \node[rounded corners=1ex,
              draw=blue,
              very thick,
              minimum height=2cm,
              minimum width=\textwidth,
              text width=\textwidth, %%% comment this line
              align=left,
              ] (a) {He she it -- das 's' muss mit.};
              \begin{pgfonlayer}{background}
                \fill[blue!20] ([yshift=-1ex]a.north east) arc(0:90:1ex) -- ([xshift=1ex]a.north west) arc(90:180:1ex) -- ([yshift=1ex]a.south west) arc(180:270:1ex) -- ([xshift=-1ex]a.south east) arc(270:360:1ex) -- cycle;
                    \fill[blue] ([yshift=-1ex]a.north west) arc(-180:-270:1ex) -- ([xshift=-1ex]a.north east) arc(-270:-360:1ex) -- ([yshift=1.5cm]a.south east) -- ([yshift=1.5cm]a.south west) -- cycle;
              \end{pgfonlayer}
              \node[anchor=west,below right,white] at (a.north west) {\bf Die Merkregel};
    \end{tikzpicture}\vspace{1ex}

%%% See the text indent …

%   \begin{tikzpicture}[remember picture,overlay]
%       \draw[ultra thick,red] ([xshift=-3.1cm]current page.south east) -- ([xshift=-3.1cm]current page.north east);
%   \end{tikzpicture}


    \begin{tcolorbox}
        He she it -- das 's' muss mit.
    \end{tcolorbox}
\end{document}

Here is the output:

Screenshot

Screenshot

(We are talking about the blue box which I created; it's on the second page of the .pdf output.)

When you comment the marked line (%%% comment this line), the width fits perfect to the text. But when you uncomment the line, the width doesn't fits anymore. So my question is: How can I fix this mistake? In particular, how can I align the node text to the left and that the width of the node fits to the text?

3
  • 3
    text width is, as the name suggests, the width of the text. One has to add inner sep, outer sep and the line width of the boundary of the box (if it has a line at its boundary). – user121799 Sep 2 '18 at 0:54
  • And specifying zero inner sep and outer sep would result in a very crowded and poorly designed text box. So perhaps you could try something like text width=0.9\textwidth. – Ruixi Zhang Sep 2 '18 at 4:43
  • 1
    unrelated but you shouldn't normally use \it or \sf in latex (they are not defined by default, although article class defines them for compatibility with documents from the 1980s) – David Carlisle Sep 2 '18 at 9:26
1

Why don't you use a tcolorbox to do this task? You are doing the other way, trying to mimmic a tcolorbox with a tikzpicture.

\documentclass[12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[ngerman]{babel}
\usepackage[table]{xcolor}
\usepackage{tikz,lipsum,libertine,microtype,colortbl,tcolorbox,amsmath,amssymb,amsfonts}
\usetikzlibrary{backgrounds}
\usepackage[explicit]{titlesec}
\newcommand{\changefont}[3]{\fontfamily{#1}\fontseries{#2}\fontshape{#3}\selectfont}

\everymath{\displaystyle}

%\titleformat{\chapter}[display]
%{\normalfont\huge\bfseries}{\filcenter\underline{\MakeUppercase{\textls[400]{\chaptertitlename}}\ \thechapter}}{20pt}{\Huge}

%\titleformat{\chapter}[display]
%{\normalfont\huge\bfseries}{\fcolorbox{red}{orange}{#1}\thechapter\hspace{1.5ex}\chaptertitlename}{20pt}{\Huge}

%\titleformat{\chapter}[display]
%{\normalfont\huge\bfseries}{
%   \begin{tcolorbox}[colframe=blue!20,colback=blue!20,skin=widget,height=3cm]
%       \vspace{2cm}
%       \hfill\color{red}#1
%   \end{tcolorbox}
%}{-10pt}{\Huge}

\titleformat{\chapter}[display]
{\normalfont\huge\bfseries}{
    \begin{tikzpicture}
        \node[fill=blue!20,minimum width=\textwidth,minimum height=2cm] (a) {};
            \node[above left] at (a.south east) {\bf\textsf{#1}};
            \node[gray] at (a.north east) {\scalebox{2}{\changefont{pnc}{m}{sl}\thechapter}};
    \end{tikzpicture}
}{-40pt}{\Huge}
\begin{document}

    \chapter{Einleitung}
    \lipsum[1]\par
    Hallo, hier steht ein Blindtext. \marginpar{\it Tipp: Versuch es selber zu lösen, dadurch lernst du mehr.} Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.
        \vspace{1cm}
    \begin{table}[h!]
    \centering
            \rowcolors{1}{blue!20}{blue!10}
        \begin{tabular}{>{\sf}r >{\sf}l}
            \rowcolor{blue!30} 
            \normalfont\textbf{Meine Arbeit} & \normalfont\textbf{Deine Arbeit} \\
            Einkauf & heute \\
            Zeichnung & morgen \\
            Arbeit & gestern \\
            Hier und da & Jetzt da und dort
        \end{tabular}
    \caption{Beispiel einer Einkaufsliste}
    \end{table}
        \thispagestyle{empty}\newpage
    \lipsum[1] Hier ist es wichtig \marginpar{\it Mit der Zeit tut man sich leichter \ldots} \lipsum[1] 

%    \vspace{1ex}\noindent\begin{tikzpicture}
%        \node[rounded corners=1ex,
%              draw=blue,
%              very thick,
%              minimum height=2cm,
%              minimum width=\textwidth,
%              text width=\textwidth, %%% comment this line
%              align=left,
%              ] (a) {He she it -- das 's' muss mit.};
%              \begin{pgfonlayer}{background}
%                \fill[blue!20] ([yshift=-1ex]a.north east) arc(0:90:1ex) -- ([xshift=1ex]a.north west) arc(90:180:1ex) -- ([yshift=1ex]a.south west) arc(180:270:1ex) -- ([xshift=-1ex]a.south east) arc(270:360:1ex) -- cycle;
%                    \fill[blue] ([yshift=-1ex]a.north west) arc(-180:-270:1ex) -- ([xshift=-1ex]a.north east) arc(-270:-360:1ex) -- ([yshift=1.5cm]a.south east) -- ([yshift=1.5cm]a.south west) -- cycle;
%              \end{pgfonlayer}
%              \node[anchor=west,below right,white] at (a.north west) {\bf Die Merkregel};
%    \end{tikzpicture}\vspace{1ex}

%%% See the text indent …

%   \begin{tikzpicture}[remember picture,overlay]
%       \draw[ultra thick,red] ([xshift=-3.1cm]current page.south east) -- ([xshift=-3.1cm]current page.north east);
%   \end{tikzpicture}

\begin{tcolorbox}[
title=Die Merkregel,
fonttitle=\bfseries,
coltitle=white,
colframe=blue,
colback=blue!20
]
He she it -- das 's' muss mit.
\end{tcolorbox}

    \begin{tcolorbox}
        He she it -- das 's' muss mit.
    \end{tcolorbox}
\end{document}

enter image description here

3
  • That's the point; it's just an exercise (or better: just for fun), I have this tcolorbox option already, but anyway thank you for that answer! +1 – current_user Sep 2 '18 at 17:28
  • @current_user tcolorbox uses pgf (TikZ) for drawing its boxes. If you look at tcolorbox code you'll know how it works and which parameters use. – Ignasi Sep 2 '18 at 18:07
  • Yeah, you're right. – current_user Sep 2 '18 at 18:35

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.