Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to add a frame to a picture. i start to draw the picture by tikz. after drawing, it is the time adding frame. now, how can i know the following two points to add the frame, assuming that frame's width is equal to the page's width:

  1. how can i get the lower left corner's coordinate of the picture?
  2. how can i get how high the picture is?

if i have these two points, i can draw a rectangle for the frame. or is any other way to realize this purpose?

@Thorsten: just adding fbox seemed not to be enough.

\documentclass[titlepage,a4paper]{article}

\usepackage{tikz}
\usepackage[lmargin=2.500000cm,rmargin=2.500000cm,tmargin=2.500000cm,bmargin=2.500000cm]{geometry}

\begin{document}
\section[General remarks]{General remarks}
\subsection[Geometry and coordinate system]{Geometry and coordinate system}
The main layout of the structure is adopted:\\

\fbox{
\begin{tikzpicture}[scale=1,thick]
  \useasboundingbox (0,0) rectangle (70mm,5);
  \begin{scope}[shift={(20mm,0)}]
    \foreach \xoffset in {0,5.2}
    {
      \begin{scope}[shift={(\xoffset,0)}]
      \draw[xstep=1,ystep=1] (0,0) grid (5,5);
      \end{scope}
    }
  \end{scope}
\end{tikzpicture}
}

\end{document}

enter image description here

based on Martin's comment, following code is added. as a newbie, it took me some time to find a way to add some spacing around picture. so this might save time for other newbies.

\documentclass[titlepage,a4paper]{article}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage[lmargin=2.500000cm,rmargin=2.500000cm,tmargin=2.500000cm,bmargin=2.500000cm]{geometry}

\begin{document}
\section[General remarks]{General remarks}
\subsection[Geometry and coordinate system]{Geometry and coordinate system}
The main layout of the structure is adopted:\\

\begin{tikzpicture}[scale=1,thick]
  \begin{scope}[shift={(20mm,0)}]
    \foreach \xoffset in {0,5.2}
    {
      \begin{scope}[shift={(\xoffset,0)}]
      \draw[xstep=1,ystep=1] (0,0) grid (5,5);
      \end{scope}
    }
  \end{scope}

  \coordinate (B) at (current bounding box.south west);
  \draw[line width=10pt]
  let
    \p2 = ($(B) - (10mm,10mm)$)
  in
  (current bounding box.north east) ++(10mm,10mm) rectangle (\p2);
\end{tikzpicture}

\end{document}

another method uses backgrounds. please refer to Andrew's comment at the below.

share|improve this question
    
Please add notes to other users as comments, not as part of the question (or an answer). Thank you. –  Martin Scharrer Mar 4 '11 at 9:35
    
@Martin: my mother tongue is not english. i am sorry that i couldn't understand your comment. do you mean, if i update my original text which is response to someone's comment, just update it and don't have to add "@someone"? –  warem Mar 5 '11 at 7:43
1  
Yes, basically. Do not add "@someone" into your updated questions. The comment you added under Thorsten's answer to point him to the update is enough. –  Martin Scharrer Mar 5 '11 at 10:39
    
@Martin: ok, got it :) –  warem Mar 5 '11 at 11:17
    
Incidentally, the reason why you got such a big whitespace in your picture on the left is because you used one of the solutions for shifting the picture that involved extending the picture invisibly. If you'd added an \hspace*{2cm} instead then the box given by fbox would be nicer than in your picture. –  Loop Space Mar 6 '11 at 8:58
add comment

3 Answers 3

up vote 13 down vote accepted

There is also the background library (not sure which version of PDF/TikZ this arrived in, it's in PGF2.10). From the manual (section 25 in PGF2.10):

This library defines "backgrounds" for pictures. This does not refer to background pictures, but rather to frames drawn around and behind pictures.

It then gives various examples, from drawing a grid behind a picture to drawing a rectangle. In the simplest case, we can just supply the option framed to the tikzpicture environment to get a simple rectangular frame. Using the background rectangle style, we can make it a little more fancy (though as the manual says, no-one in their right mind would use this particular framing).

Code:

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{backgrounds}

\begin{document}
\begin{tikzpicture}[framed]
\draw (0,0) circle (2);
\draw (0,0) rectangle (3,2);
\end{tikzpicture}

\bigskip

\begin{tikzpicture}[framed,background rectangle/.style={double,ultra thick,draw=red, top color=blue, rounded corners}]
\draw (0,0) circle (2);
\draw (0,0) rectangle (3,2);
\end{tikzpicture}
\end{document}

Picture:

framed tikzpicture

There are, of course, several options to change the background, in particular to change how far from the picture the frame is. See Section 25 of the manual (2.10 version) for details.

share|improve this answer
    
Nice! I knew backgrounds before but was not aware of the framed and other options it provides. –  Martin Scharrer Mar 4 '11 at 10:17
    
i noticed backgrounds in pgfmanual literally and didn't look it through. it seemed that i missed powerful function. thanks a lot. –  warem Mar 5 '11 at 7:21
    
based on your example, if i want to add one frame box for the two pictures, how to do it by backgrounds? backgrounds seemed to work on one tikzpicture environment once. –  warem Mar 5 '11 at 10:43
    
@warem: Yes, backgrounds works on one tikzpicture at a time. You have a couple of alternatives. You could put your two pictures in to one (you could separate them with \begin{scope} ... \end{scope}) or you could add your pictures as the contents of two nodes in a container picture and put the frame on that one. Of the two, I'd go for the former route as it's cleaner. –  Loop Space Mar 5 '11 at 17:55
    
i prefer to the former route. –  warem Mar 6 '11 at 0:46
add comment

You can get the size of the current picture using the special rectangle node current bounding box. To frame the whole picture add the following code at the very end:

\draw (current bounding box.north east) rectangle (current bounding box.south west);

If you want to save this coordinates (which can also be used to calculate the height) see the question How can I save the bounding box of a TikZpicture and use in other TikZpicture.

share|improve this answer
    
current bounding box is also a way. thank you. –  warem Mar 5 '11 at 11:19
add comment

The simplest would be to just use a framebox.

\fbox{%
  \begin{tikzpicture}
  % picture content
  \end{tikzpicture}
}
share|improve this answer
    
please have a look at my original text. it is updated by an example. –  warem Mar 4 '11 at 9:17
1  
@warem: I you want to make the framed box (\fbox) exactly around the tikzpicture set the length \fboxsep to 0pt. –  Martin Scharrer Mar 4 '11 at 9:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.