3

In my current document I have both pictures from file and tikz pictures. For the output I use both, landscape (for screen reading) and portrait (for print).

For pictures loaded from file i use the code blow to make sure that they are always of an acceptable size.

\begin{figure}
\centering
\includegraphics[keepaspectratio,  width=1\textwidth, height=1\textheight]{Filename}
\caption{Figure}
\end{figure}

The problem is that I have not been able to apply the same to my tikx picture as it does not have a dimension by it self. I am using $width=1\textwidth, height=1\textheight$ but that just means that the picture will have a different aspect ratio in portrait and landscape mode.

Is there a simple way to define the aspect ratio (e.g 4:3) of a tikx picture?

Edit 1

I thought I had found a simple solution.

Other than in my example where the height is limited by the textheigt and the width beeing limited by the textwidth I use the same dimension for both, height and width for tikx pictures. The only difference is that I use different factors. Below a short example.

\begin{tikzpicture}
\begin{axis}[
        width=0.80\textwidth,
        height=0.60\textwidth,
        ...

With this setup will always have an aspectratios of 4:3 and a width of 80% of the text. The problem however is that with other values the image might get to heigh to fit on one page (e.g. for aspect ratio 1:1).

Any suggestions?

2
  • 2
    There is the tikzscale package
    – percusse
    Apr 10, 2014 at 14:54
  • 1
    @precusse That package is only for absolut scaling of tikz images. I would however prefer to set an aspect ratio and a relative maximum height and width. With this the image should always look similar - regardless of paper orientation - and fit on the page.
    – MatoBehr
    Apr 10, 2014 at 15:43

2 Answers 2

1

adjustbox package can be used to change the size of any kind of contents, including TiKZ figures.

\documentclass{article}
\usepackage{adjustbox}
\usepackage{tikz}

\begin{document}
  \def\test{\tikz\draw[thick, green] (0,0) rectangle (2cm,1cm) node[midway,red] {Test};}

  % the original 2cm x 1cm
  \test

  % resized to 4cm x 2cm
  \minsizebox{4cm}{2cm}{\test}

  % keeps aspectratio
  \minsizebox{1cm}{5cm}{\test}

  \minsizebox{4cm}{1cm}{\test}

\end{document}

enter image description here

2
  • Why the last one is 2cm heigh ? It must fit in 4cm x 1cm, no ?
    – Kpym
    Dec 18, 2014 at 15:04
  • Ok, after reading the doc it may looks normal for \minsizebox because it adjust only the height, not the total height, but even with \minsizebox* (which adjust the total height) it fails. And moreover the depth of the test picture is 0, I think. Strange.
    – Kpym
    Dec 18, 2014 at 15:16
0

Here is a solution to scale an image using scalebox in a way to fit it in a desired size (and keeping the aspect ratio).

\documentclass[varwidth,border=50]{standalone}
\usepackage{environ}
\usepackage{tikz}
\usetikzlibrary{math}

% --------------------- begin macro definition
\newsavebox{\tikzbox}
% fitin environment
\NewEnviron{fitin}[2]{
\savebox{\tikzbox}{\BODY}
\tikzmath{\scale = min(#1/\wd\tikzbox, #2/(\ht\tikzbox+\dp\tikzbox));}
\scalebox{\scale}{\usebox{\tikzbox}}
}
% --------------------- end macro definition
\begin{document}

  % the original 2cm x 1cm
  \tikz \fill[green] (0,0) rectangle (2cm,1cm);

  % fit in  8cm x 2cm => 4cm x 2cm
  \begin{fitin}{8cm}{2cm}
    \tikz \fill[green] (0,0) rectangle (2cm,1cm);
  \end{fitin}

  % fit in  8cm x 8cm => 8cm x 4cm
  \begin{fitin}{8cm}{8cm}
    \tikz \fill[green] (0,0) rectangle (2cm,1cm);
  \end{fitin}

\end{document}

enter image description here

EDIT: The first solution is not tikz specific, and here is better sulution without scalebox that use the standard tikz scale.

\documentclass[varwidth,border=50]{standalone}
\usepackage{environ}
\usepackage{tikz}

% --------------------- begin macro definition
\newsavebox{\tikzbox}
% tikzfitin environment
\NewEnviron{tikzfitin}[2]{
\savebox{\tikzbox}{\BODY}
\tikzset{every picture/.style={scale = {min(#1/\wd\tikzbox, #2/(\ht\tikzbox+\dp\tikzbox))}}}
\BODY
}
% --------------------- end macro definition
\begin{document}
  \def\test{\tikz\draw[thick, green] (0,0) rectangle (2cm,1cm) node[midway,red] {Test};}

  % the original 2cm x 1cm
  \test

  % fit in  8cm x 2cm => 4cm x 2cm
  \begin{tikzfitin}{8cm}{2cm}
    \test
  \end{tikzfitin}

  % fit in  8cm x 8cm => 8cm x 4cm
  \begin{tikzfitin}{8cm}{8cm}
    \test
  \end{tikzfitin}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .