COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Package to allow scaletowidth of arbitrary content

• Email
• Follow

Hi,
quite often I need to scale a complex TeX piece (e.g. a tikzpicture or
similar) to a certain width (e.g. .9\textwidth).
I couldn't find a general solution for this. The 'textfit' package
does something similar, but apparently only for text.
So I coded a small macro which uses the power of the 'pgfmath' and
'graphicx' packages to calculate the necessary scale factor and scale
it appropriately.

I would like to share the result with the rest of the TeX community.
Please do not hesitate to contact me if you have any feedback on it.
There are with no doubt a couple of alternative ways to calculate the
scaling factor. I'm using 'pgfmath' because it's simple to use and

The code can be found also at http://latex.scharrer-online.de/general/attachment/wiki/scaletowidth

%%%%% BEGIN OF CODE %%%%%%

% Copyright (c) 2010 by Martin Scharrer <martin@scharrer-online.de>
% http://latex.scharrer-online.de/general/attachment/wiki/scaletowidth
% This is Free Software under the LPPL 1.3c or later licence.
%
% Description:
% This package provides the macro \scaletowidth which scales a given
content
% down or up to match a given width.
%
% Usage:  \scaletowidth{<width>}{<any TeX content>}
%
% Notes:
% Please note the content is processed inside a TeX group, making all
included
% changes local.
%
% Examples:
% Scaling a text to match the full line width:
%
% Here also \textwidth can be used instead of \linewidth. Please note
that this
% will most likely result in an odd effective font size.
%
% Scaling a drawing (picture, pstricks or  tikz picture etc.) to a
given width
% (e.g. 90% of the text width). Here the drawing is saved inside a own
TeX file
% (drawing.tex), but could also be placed in the place of the \input
command.
%   \scaletowidth{.9\textwidth}{\input{drawing}}
%
% Similar Packages:
% textfit
%
% Implementation:
% Stores the content in a temporary TeX box and calculates the ratio
between the
% requested width and the current width using the pgfmath package.
The \scalebox
% macro from the graphicx package is used to scale the box
appropriately.
% While a different implementation with fewer or different
dependencies is possible,
% this form was chosen due to its quick and robust form.
%
\ProvidesPackage{scaletowidth}[2010/03/03  v1.0  Scales content to
given width.]%
%
\RequirePackage{graphicx}%
\RequirePackage{pgfmath}%
%
\def\scaletowidth#1#2{%
\begingroup
\sbox{\@tempboxa}{#2}%
\pgfmathsetmacro{\@tempdima}{#1 / \wd\@tempboxa}%
\scalebox{\@tempdima}{\usebox{\@tempboxa}}%
\endgroup
}%
\endinput

%%%%% END OF CODE %%%%%%

 0
Reply martin.scharrer (98) 3/3/2010 4:47:27 PM

See related articles to this posting

On Mar 3, 4:47=A0pm, Martin Scharrer <martin.schar...@gmail.com> wrote:
> The code can be found also athttp://latex.scharrer-online.de/general/atta=
chment/wiki/scaletowidth

http://latex.scharrer-online.de/general/wiki/scaletowidth

Best Regards,
Martin

 0

On Mar 3, 4:47=A0pm, Martin Scharrer <martin.schar...@gmail.com> wrote:
> Hi,
> quite often I need to scale a complex TeX piece (e.g. a tikzpicture or
> similar) to a certain width (e.g. .9\textwidth).
> I couldn't find a general solution for this. The 'textfit' package
> does something similar, but apparently only for text.
> So I coded a small macro which uses the power of the 'pgfmath' and
> 'graphicx' packages to calculate the necessary scale factor and scale
> it appropriately.
>
> I would like to share the result with the rest of the TeX community.
> Please do not hesitate to contact me if you have any feedback on it.
> There are with no doubt a couple of alternative ways to calculate the
> scaling factor. I'm using 'pgfmath' because it's simple to use and

I just found out that there is a \resizebox macro provided by the
'grahics' package which can be used for this.
Funny I always find this things AFTER I wrote and posted my own code,
but never before. I only searched for 'scaletowidth' and similar
expressions, but not for 'resize'. :-(

Martin


 0