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 am using TikZ to draw standalone pictures using the PreviewEnvironment (which cuts the PDF size around the picture). I then import the PDF figure inside LaTeX using \includegraphics.

Let say my font size in LaTeX is 12pt. I want to bring in a PDF figure with a good looking size and such that the size of the text in the figure and LaTeX text are same. For this I need to determine and set

  1. the Canvas size (width and highs of the figure in mm),
  2. the font size,

in the TikZ. How can I do that?


Thank you all who responded.

I tried to play around with your suggestions.

*The idea is to create a standalone TikZ code for some drawing, such that you can create proper PDF files of any given size from that and import them in any LaTeX document. The pictures should have 1) an adjustable test (font) size ( to set based on your main document), 2) independent of the text size, an adjustable line thickness (to change for an optimum visual presentation) and, of course, 3) adjustable bounding box (i.e. scaling coordinates, distance between objects, or anything except 1 and 2) *

Now as you start drawing a picture, you do not know what values you should choose for your objects and what would be the dimensions of the output PDF as a result. I start choosing most of the values (radius of circles etc) to be 1cm and at the end I look at File>Properties, and the dimension of the PDF is 234mm*142mm! Now I cannot go back to change all those values, so I will have to scale this. If you use \usetikzpiture[scale=0.2], it will not scale line thickness, and you will have to change this manually. If you use \pgftransformscale or \resizebox that scales everything, making the text too small too. If you use \path [use as bounding box], that just crops the pdf and you have to change everything to make sure it fits inside (specially later on when re-drawing the picture at a different dimension).

So in short, you want to set font size, line thicknesses, and bounding box dimensions independent of each other. Then I can adjust the same picture in various sizes all look ok with proper text size.

The easiest way I can think of is to set a variable, linethickness, at the beginning to adjust line widths and then use scale=... to scale the picture. I set the font size in the documentclass.

Since I am new to TikZ, I do not know if I am doing things correctly. Maybe standalone PDFs are not a good idea and better to use Tikz code inside latex directly.

Thank you

share|improve this question
1  
To fix the canvas size to, say, 5cm by 4cm, you can just say \path [use as bounding box] (0,0) rectangle (5cm,4cm); at the beginning of your tikzpicture. This will fix the size of the bounding box and therefore of the standalone picture. However, you'll then have to make sure that all the objects you draw are actually inside this area. The font size should automatically be the same as in your main document (unless you use different options in the documentclass). All in all, this question is not very clear, could you maybe add a concrete example where things aren't working as you'd like? –  Jake Jun 27 '12 at 6:21
    
There is no problem if you work in the spirit of TikZ. You can fix the size of your picture with the bounding box (Jake's comment) or a clipand then you can fix the size of a font. Personally I prefer to avoid importing PDF but it's bad idea to modify the size of the PDF. This is a very bad way. Better is to (re) create the picture with the good canvas size. The problem is different if the pictures are created by another user and if you don't know the bounding box and the size of the font used. –  Alain Matthes Jun 27 '12 at 7:23
    
why is it a bad idea? what is the correct way of using TikZ for the purpose that I mentioned? Thanks –  eli Jun 28 '12 at 16:18
    
Thanks Jake. I added a clarification, please see below –  eli Jun 28 '12 at 16:18
1  
@eli: Please do not ask question in the answer section. For question about a particular answer add a comment to that answer; for additional information on your question, edit it; for asking a new unrelated question please ask a separate question. –  Caramdir Jun 28 '12 at 16:39
show 1 more comment

2 Answers 2

For the sizes, you can use the current bounding box together with \pgfgetlastxy:

\documentclass{standalone}
\usepackage{tikz}

\newcommand*{\ExtractCoordinate}[3]{\path (#1); \pgfgetlastxy{#2}{#3};}%

\newdimen\tlx
\newdimen\tlx
\newdimen\brx
\newdimen\bry


\begin{document}
\begin{tikzpicture}
    \draw (-3,-1) rectangle (3,1) (-2,-2) rectangle (2,2);
    \foreach \x in {north west,south west,south east,north east}
    {   \fill[red] (current bounding box.\x) circle (0.01);
    }
    \ExtractCoordinate{current bounding box.north west}{\tlx}{\tly}
    \ExtractCoordinate{current bounding box.south east}{\brx}{\bry}
    \pgfmathsetmacro{\myheight}{(+\tly-\bry)/28.452755}
    \pgfmathsetmacro{\mywidth}{(-\tlx+\brx)/28.452755}
    \node[below right] at  (-2,1) {height: \myheight\ cm};
    \node[above left] at  (2,-1) {width: \mywidth\ cm};
\end{tikzpicture}
\end{document}

enter image description here


Edit 1: Ah, now I get it. Here is a still not automatic version. You set two commands in each tikzpicture: \maximumdimensions{4}{3}, which is the desired width and height, at the beginning of the picture. \getscalingfactors which will draw nodes with a recommended scaling factor in the middle of your picture if the image if to high/wide:

\documentclass{scrartcl}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage{xifthen}

\newcommand*{\ExtractCoordinate}[3]{\path (#1); \pgfgetlastxy{#2}{#3};}%

\newdimen\tlx
\newdimen\tlx
\newdimen\brx
\newdimen\bry
\def\xscalefactor{}
\def\yscalefactor{}
\parindent0mm

\newcommand{\getscalingfactors}{%
    \ExtractCoordinate{current bounding box.north west}{\tlx}{\tly}
    \ExtractCoordinate{current bounding box.south east}{\brx}{\bry}
    \pgfmathsetmacro{\myheight}{(+\tly-\bry)/28.452755}
    \pgfmathsetmacro{\mywidth}{(-\tlx+\brx)/28.452755}
    \pgfmathsetmacro{\xsf}{\maxwidth/\mywidth}
    \pgfmathsetmacro{\ysf}{\maxheight/\myheight}
    \global\edef\xscalefactor{\xsf}
    \global\edef\yscalefactor{\ysf}
    \pgfmathtruncatemacro{\xsfc}{\xsf*10000}
    \pgfmathtruncatemacro{\ysfc}{\ysf*10000}
    \ifthenelse{\xsfc > 10000}{}{\node[fill=white,above] at (current bounding box.center) {x-s: \xsf};}
    \ifthenelse{\ysfc > 10000}{}{\node[fill=white,below] at (current bounding box.center) {y-s: \ysf};}
}

\newcommand{\maximumdimensions}[2]{% width, height
\global\edef\maxwidth{#1} % in cm
\global\edef\maxheight{#2} % in cm
}

\begin{document}

\begin{tikzpicture}[scale=1]
    \maximumdimensions{4}{3}

    \draw[fill=orange] (-3,-1) rectangle (3,1) (-2,-2) rectangle (2,2);

    \getscalingfactors
\end{tikzpicture}

\lipsum[1]

\begin{tikzpicture}[scale=1]
    \maximumdimensions{4}{3}

    \draw[fill=orange!50!gray] (0,0) circle (3);
    \draw[fill=red!50!gray] (3,1) circle (1);
    \draw[fill=blue!50!gray] (-2,2) circle (1);
    \draw[fill=green!50!gray] (-3,-2) circle (1);
    \draw[fill=yellow!50!gray] (2,-1) circle (1);

    \getscalingfactors
\end{tikzpicture}

\end{document}

enter image description here

If you scale the picture down enough, the nodes inside the picture vanish: \begin{tikzpicture}[scale=0.66] and \begin{tikzpicture}[scale=0.497]

enter image description here

share|improve this answer
    
Maybe I'm misunderstanding the OP completely, but I think when they say "I need to determine and set the Canvas size (width and highs of the figure in mm)", they mean that they want to adjust the size of the figure by specifying its height and width (using mm) so it takes up a certain amount of space in the main document, not print the size. Otherwise, I can't see how merely knowing (without influencing) the dimensions of the figure would help them to "bring in a PDF figure with a good looking size ". –  Jake Jun 27 '12 at 6:33
    
That's right. I want to adjust the dimensions, not to print their values. But using the standalone class is nice; i did not know that. How do you print the \textwidth in LaTeX? I need that to calculate the dimensions of the external graph. –  eli Jun 28 '12 at 16:22
    
You can use \the\textwidth to print the textwidth in pt. Alternatively, \pgfmathsetmacro{\ininch}{\the\textwidth/72.27} \ininch\ inch\\pgfmathsetmacro{\incm}{\the\textwidth/28.452755} \incm\ cm for inch and centimeter –  Tom Bombadil Jun 28 '12 at 17:55
add comment

You may calculate coordinates of your nodes:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,positioning}
\usepackage{lipsum}
\usepackage{multicol}
\newcommand\mypicture[1]{
  \noindent{\centering\begin{tikzpicture}
    \path (0,0) -- (#1,0)
    node[pos=0,anchor=base west](n1) {first}
    node[pos=.5,anchor=base] (n2){second}
    node[pos=1,anchor=base east](n3){third}
    ;
    \draw[->] (n1) -- (n2);
    \draw[->] (n2) -- (n3);
    \begin{pgfonlayer}{background}
      \fill[red!10] (current bounding box.south west)
      rectangle  (current bounding box.north east);
    \end{pgfonlayer}
  \end{tikzpicture}\par}
}
\begin{document}
\lipsum[1]\par
\mypicture{\linewidth}
\mypicture{.5\linewidth}
\lipsum[4]\par
\begin{multicols}{2}
\lipsum[9]\par
\mypicture{\linewidth}
\mypicture{.8\linewidth}
\lipsum[4]\par
\end{multicols}
\end{document}

enter image description here

share|improve this answer
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.