4

I need to use a tikz picture and some text with equations, inside a frame or something like this, to add an highlighted example in my page. I tried using tcolorbox but it doesn't allow to use tikzpicture inside. Here is the code I want to highlight:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{positioning}

This is the an example text:
\begin{equation}
1=2
\end{equation}

\begin{figure}
\centering
\begin{tikzpicture}
\node at (0,0) (a) {a};
\node at (1,0) (b) {b};
\draw (a)--(b);
\end{tikzpicture}
\end{figure}

\end{document}

I need the image below the text, minipages are not a solution. Any answer helps, thanks!

  • I'm a bit off-topic, but \centering{} does not really make sense as \centering is a switch and does not take any argument. Also, there's a typo: \enf{figure}. Compilable code would make helping you easier. o( +_+ )o – Alice M. Aug 3 '17 at 20:50
  • You want all of this inside the same box? If so, you can't have part of your content within a figure environment (which is a float) and part without. – Michael Palmer Aug 3 '17 at 20:52
  • Welcome to TeX.SX! Please help us (and also you) and add a minimal working example (MWE), that illustrates your problem. Somethink like this: Example: Boxes with text and math ? – Bobyandbob Aug 3 '17 at 20:53
  • @AliceM. \centering is centering the picture in my page. Thanks for the comment! – Federico Caccia Aug 3 '17 at 20:56
  • @FedericoCaccia Of course, but there is no need for those braces. They are confusing since they do not reflect the way the command is meant to be used, and they serve no real purpose. – Alice M. Aug 3 '17 at 21:00
6

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}

enter image description here

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}

enter image description here

  • Just for sake of completeness: tikzpicture can be used inside a tcolorbox without restrictions - there is no need for the listings way (but, of course, this works). – Thomas F. Sturm Aug 4 '17 at 7:44
  • @ThomasF.Sturm, You are right, I have added a version without listings. – StefanH Aug 4 '17 at 7:57
7

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}

enter image description here

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

  • amazing, it helps me! I really appreciate your time! – Federico Caccia Aug 3 '17 at 21:14
4

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}

text in a box

  • Welcome! Croeso! – cfr Aug 3 '17 at 22:11

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.