[Tex/LaTex] Keepaspectratio option for tikz picture

scalingtikz-pgf

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?

Best Answer

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