I would like to scale an image file for each dimension differently for example 50% of the x dimension or 25% of the y dimension. Thank you very much.
[Tex/LaTex] how to scale each dimension of an image with different ratio
graphicsscaling
Related Solutions
[Tex/LaTex] Scale included graphics to the higher ratio instead of the lower for given width, height
The newest version of adjustbox
from 2011/08/07 provides min width
, min height
, min size
and also max ...
as well. These keys ensure that the given dimension is at least or at most the given length and scale it up or down if required, respectively. They always keep the aspect ratio.
I think the min size={<width>}{<height>}
is what you need. It uses the larger scaling factor, as you requested. If you have an image which is already larger you need to use max size
beforehand with a smaller size first to ensure it is scaled down. This doesn't has any effect on the final image resolution because the whole image is included in the PDF unchanged and then shown scaled by the PDF viewer. Each scaling only adds a little rounding error if the resulting factor isn't an integer.
When loaded with the export
option adjustbox
exports these key also to \includegraphics
. It also provides a Clip
and Clip*
keys (note the capital C
) to clip the content after any scaling etc. You can use \width
and \height
to refer to the current size. Clip*
awaits the clip viewport, so use .5\width-.5\yourlength
, .5\width+.5\yourlength
, etc. to clip from the center:
\documentclass{standalone}
\usepackage[export]{adjustbox}
\begin{document}
\includegraphics{image}
\includegraphics[width=10cm,height=10cm,keepaspectratio]{image}
\includegraphics[min size={10cm}{10cm}]{image}
\includegraphics[min size={10cm}{10cm},Clip*={.5\width-5cm} {.5\height-5cm} {.5\width+5cm} {.5\height+5cm}]{image}
\end{document}
There are a few things that scaling doesn't affect; the most noticeable are node sizes and line widths. In a simple picture, it isn't hard to adjust the line width accordingly but the nodes can be difficult. It is possible to force a node to be scaled: put the scale option directly in the node's attributes. Thus \node[above,scale=0.6] at (8,11) {true};
would scale the node. This is a bit annoying to put on every node, so there's an every node
style that can be used to do this. Thus:
\begin{tikzpicture}[thick,scale=0.6, every node/.style={scale=0.6}]
Even so, you'd still have to remember to change two things each time here if you wanted to change the scale factor. Fortunately, there's a key transform shape
which means that the current transformation is applied to the node. The danger with using this is that this will also apply any rotations that happen to be in effect to the node (normally only translations are applied). If you don't have any rotations, then:
\begin{tikzpicture}[thick,scale=0.6, every node/.style={transform shape}]
will do just fine.
If you do have or worry about those rotations (or for anyone else interested) it would be simple to set a global scale
key to fix this:
\tikzset{global scale/.style={
scale=#1,
every node/.style={scale=#1}
}
}
Back to the "normal" solutions. Here's the various solutions:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\framebox{\begin{tikzpicture}[thick]
\draw [dashed] (1,12) -- (11,12);
\node[above] at (4,11) {1}; \node[above] at (8,11) {true};
\end{tikzpicture}}
\framebox{\begin{tikzpicture}[thick, scale=0.6]
\draw [dashed] (1,12) -- (11,12);
\node[above] at (4,11) {1}; \node[above] at (8,11) {true};
\end{tikzpicture}}
\framebox{\begin{tikzpicture}[thick, transform canvas={scale=0.6}]
\draw [dashed] (1,12) -- (11,12);
\node[above] at (4,11) {1}; \node[above] at (8,11) {true};
\end{tikzpicture}}
\framebox{\begin{tikzpicture}[thick,scale=0.6, every node/.style={scale=0.6}]
\draw [dashed] (1,12) -- (11,12);
\node[above] at (4,11) {1}; \node[above] at (8,11) {true};
\end{tikzpicture}}
\framebox{\begin{tikzpicture}[thick,scale=0.6, every node/.style={transform shape}]
\draw [dashed] (1,12) -- (11,12);
\node[above] at (4,11) {1}; \node[above] at (8,11) {true};
\end{tikzpicture}}
\end{document}
I put the \framebox
s in because if you count carefully, you'll see that the third example isn't there! In fact, it ended up somewhere at the top of the page, outside what the standalone
package thought the page was. So it got clipped out.
Best Answer
There are several ways to go about this.
In the
graphicx
package you can specify:You can also use
Also there's