Tikz picture inside a box
I might have misunderstood the question, but here is a try:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{figure}
\centering%
\begin{tikzpicture}
\node at (0,0) (a) {a};
\node at (1,0) (b) {b};
\draw (a)--(b);
\node[above = \baselineskip of current bounding box] {%
\begin{minipage}{0.9\linewidth}
Text or whatever you want to put here.
\begin{equation*}
1 = 2
\end{equation*}
\end{minipage}%
};
\draw (current bounding box.north west) rectangle (current bounding box.south east);
\end{tikzpicture}
\end{figure}
\end{document}
Quick explanations: once the main elements of the picture are drawn, I create a node that gets positioned right on top of everything (above the “current bounding box”, by a distance relative to \baselineskip
because it's slightly less dirty). Finally, I draw a rectangle around the final bounding box. … It's a bit overkill, now that I think about it.
By the way, I'm pretty sure tikzpictures can be put in tcolorboxes, since I used tcolorboxes to show examples in the documentation of a pseudo-package I wrote, and these were full of tikz stuff: http://www.alicem.net/files/oths/pointbox.zip
You can include tikzpicture
in tcolorbox
, at least if you use listings
. I know that you said not to use minipage
, but not why noy to use it. To me this is the natural start of such a problem, so I included a solution for that as well. And I expanded the MWE with some lipsum
text and boxes around nodes.
\documentclass{article}
\usepackage{lipsum}
\usepackage{calc}
\usepackage{tikz}
\usepackage[listings]{tcolorbox}
\newtcblisting{EvalBox}[2][]{%
colback=white,
arc=0pt,
boxrule=0.5pt,
text only,
title=#2,#1}
%%
\begin{document}
\lipsum[1]
%% First
\begin{EvalBox}{}
\lipsum[2]
\begin{equation}
1=2
\end{equation}
\centering
\begin{tikzpicture}
\node[draw] at (0,0) (a) {a};
\node[circle,draw] at (1,0) (b) {b};
\draw[->] (a)--(b);
\end{tikzpicture}
\end{EvalBox}
%% Second version, using minipage
\noindent
\fbox{\begin{minipage}[t]{1.0\linewidth-2\fboxsep-2\fboxrule}
\lipsum[2]
\begin{equation}
1=2
\end{equation}
\centering
\begin{tikzpicture}
\node[draw] at (0,0) (a) {a};
\node[circle,draw] at (1,0) (b) {b};
\draw[->] (a)--(b);
\end{tikzpicture}
\end{minipage}}
\end{document}
EDIT
A version with tcolorbox
not using listing
can also be done:
\documentclass{article}
\usepackage{lipsum}
\usepackage{tikz}
\usepackage{tcolorbox}
\begin{document}
\lipsum[1]
\begin{tcolorbox}[colback=white]
\lipsum[2]
\begin{equation}
1=2
\end{equation}
\centering
\begin{tikzpicture}
\node[draw] at (0,0) (a) {a};
\node[circle,draw] at (1,0) (b) {b};
\draw[->] (a)--(b);
\end{tikzpicture}
\end{tcolorbox}
\end{document}
Here is something that doesn't use minipages (for this particular problem).
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{positioning, fit}
\begin{document}
\begin{figure}
\centering%
\begin{tikzpicture}
\node [text width = 0.9\linewidth] (first) {
Text or whatever you want to put here.
};
\node [below=\baselineskip] at (first) (eq) {$1=2$};
\node [below left=\baselineskip] at (eq) (a) {a};
\node at ([xshift=1cm] a) (b) {b};
\draw (a)--(b);
\node [draw, rectangle, fit=(first)(eq)(a)(b)]{};
\end{tikzpicture}
\end{figure}
\end{document}