8

Sorry if this has been asked before – I vaguely remember seeing something related to this, but can't find it anymore.

So I took a TeXample from here: Boxes with text and math. I edited it a tiny bit to make it shorter. Now, the problem is that although I specified the width of the box to be equal to the \textwidth, it seems to be a bit larger than the area from the left to the right margin, and it also shifted to the right (as is made clear from the showframe package. What it wrong here?

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes,decorations}
\usepackage{amsmath,amssymb}
\usepackage{showframe}

\begin{document}

\tikzstyle{mybox} = [draw=red, fill=blue!20, very thick,
    rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
\tikzstyle{fancytitle} =[fill=red, text=white]

\begin{tikzpicture}
\node [mybox] (box){%
    \begin{minipage}{\textwidth}
        Some text and math blah blah blah
        \begin{align}
            \dot{n} &= u\cos\psi -v\sin\psi \\
            \dot{e} &= u\sin\psi + v\cos\psi
        \end{align}
    \end{minipage}
};
\node[fancytitle, right=10pt] at (box.north west) {A fancy title};
\node[fancytitle, rounded corners] at (box.east) {$\clubsuit$};
\end{tikzpicture}%

\end{document}

enter image description here

Skeleton Bow
  • 1,179

2 Answers2

8

You have set the inner sep = 10pt, so you should subtract double that value from the width of the minipage, besides \noindent is also required.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,decorations}
\usepackage{amsmath,amssymb}
\usepackage{showframe}

\begin{document}

\tikzstyle{mybox} = [draw=red, fill=blue!20, very thick,
    rectangle, rounded corners, inner sep=10pt, inner ysep=20pt]
\tikzstyle{fancytitle} =[fill=red, text=white]

\noindent\begin{tikzpicture}
\node [mybox] (box){%
    \begin{minipage}{\dimexpr\textwidth-20pt}
        Some text and math blah blah blah
        \begin{align}
            \dot{n} &= u\cos\psi -v\sin\psi \\
            \dot{e} &= u\sin\psi + v\cos\psi
        \end{align}
    \end{minipage}
};
\node[fancytitle, right=10pt] at (box.north west) {A fancy title};
\node[fancytitle, rounded corners] at (box.east) {$\clubsuit$};
\end{tikzpicture}%

\end{document}

enter image description here

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
5

A tcolorbox is, by default, as wide as \textwidth, so you don't have to worry about its size and placement.

\documentclass{article}

\usepackage[most]{tcolorbox}
\usepackage{amsmath,amssymb}
\usepackage{showframe}

\newtcolorbox{mybox}[2][]{%
    enhanced,
    title = #2, 
    attach boxed title to top left={%
        xshift=5mm, 
        yshift=-\tcboxedtitleheight/2, 
        yshifttext=-1mm},
    boxed title style={colback=red, sharp corners},
    colframe = red,
    colback = blue!20,  
    overlay = {\node[text=white, fill=red] at (frame.east) 
        {$\clubsuit$};},
    #1}


\begin{document}

\begin{mybox}{A fancy title}%
        Some text and math blah blah blah
        \begin{align}
            \dot{n} &= u\cos\psi -v\sin\psi \\
            \dot{e} &= u\sin\psi + v\cos\psi
        \end{align}
\end{mybox}

\end{document}

enter image description here

Ignasi
  • 136,588