LaTeX, Standalone
LaTeX, Standalone
Martin Scharrer
[email protected]
CTAN: http://www.ctan.org/pkg/standalone
VC: https://bitbucket.org/martin_scharrer/standalone
Abstract
1
1 Online Resources and Help
This bundle is released on the comprehensive TEX archive network (CTAN) under
https://ctan.org/pkg/standalone. Any modern LATEX distribution should pick it
from there automatically, though. CTAN now allows votes and comments on
packages and such feedback is very welcome.
The source code of this bundle is hosted on Bitbucket as Mercurial repository
under https://bitbucket.org/martin_scharrer/standalone/. The ticket system
there should be used to report bugs or request new features: https://bitbucket.
org/martin_scharrer/standalone/issues/new.
User which need help with should search for an existing solution on the
site TeX.Stackexchange.com: https://tex.stackexchange.com/questions/tagged/
standalone or post a new question: https://tex.stackexchange.com/questions/
ask?tags=standalone. The bundle author is an active user on TeX.SE but other
users might help as well.
2 Installation
This bundle is part of the two main LATEX distributions TeX Live and MikTeX and
can be easily installed with their package managers. It is also provided as TDS
ZIP file on CTAN which can be used for a manual install. Other packages are also
required as described in section 2.4.
2
install. A manual install is only required if the used distribution does not include
the (required version of the) package.
The TDS (TEX Directory Structur) ZIP file includes all package files in the final form
and relative location. It can be downloaded from CTAN: http://mirrors.ctan.org/
install/macros/latex/contrib/standalone.tds.zip and from the Bitbucket project
site https://bitbucket.org/martin_scharrer/standalone/downloads/standalone.
tds.zip. It needs only to be unzipped in a TEXMF directory. Under Linux/Unix
this is usually the user TEXMF directory ~/texmf. Under Windows it is usu-
ally C:\Users\<username>\texmf (Windows Vista/7) or C:\Documentsand%
Settings\<username>\texmf (Windows XP). Under Mac OS X it is usually
/Users/<username>/Library/texmf. Alternatively a system local directory
can be used which is usually given by the environment variable TEXMFLOCAL.
After the files are copied to this location the file name database of TEX might need
to be updated. This can be done with TeX Live by running texhash <path> or
mktexlsr <path>. MikTeX provides a graphical interface to refresh its file name
database as described by http://docs.miktex.org/manual/configuring.html#fndbupdate.
The package is also provided as DTX (Documented TeX file) which is accom-
panied by an INS (Install) file under http://www.ctan.org/tex-archive/macros/
latex/contrib/standalone. To unpack all package files from the DTX file compile
the INS file with tex once. The manual can be compiled from the DTX file with
pdflatex. This requires the ydoc bundle to be installed.
2.4 Dependencies
The standalone class and package require the xkeyval package. The packages
ifpdf, ifluatex, ifxetex, and shellesc are loaded if available, otherwise
some fall-back code is used. If enabled the class options varwidth, preview and
beamer require the package or class of the same name.
The standalone package requires the currfile package (which in turn uses
filehook) to track the correct file names of sub-files included using \input. For
the compilation support for included standalone files the gincltex and filemod
packages are also required.
To compile the documentation of standalone the ydoc bundle is required.
All of these packages are included in recent versions of the TeXLive or MikTeX
distributions and are freely available on CTAN.
3
3 Bug reports, feature requests and other feedback
Bug reports, feature requests and other feedback about the standalone bundle
can be sent to the author either by email to [email protected] or using
the issue tracker for the bundle under https://bitbucket.org/martin_scharrer/
standalone/issues. Bug reports should include the used version of standalone as
well as the used LATEX format (pdflatex, latex, xelatex, etc.) and distribution
including its version. Usually a minimal example which recreate the issue is
immensely helpful in analysing and solving any bug. Please look for existing
related issue tickets first and check the FAQ/troubleshooting in section 5.8 first.
Issues related to the preview class option should be compared with a direct use of
the underlying preview package.
4
4 Introduction
Larger LATEX documents can be split into multiple TEX files which are then in-
cluded in a main document with \include for e.g. chapter files or \input for e.g.
TEX-coded pictures. Keeping pictures in their own sub-files improves readability
of the main file and simplifies the sharing of them between different documents.
However, during the, sometimes lengthly, drawing/coding process it has benefits
to be able to compile the pictures on their own. The compile process is much
quicker and the resulting document only holds the picture which avoids constant
page turning and zooming.
While it is possible to write a small ‘main’ file for each picture file, this method
is a little cumbersome and clutters the directories with a lot of extra files. A
second method is to place the ‘main’ components, i.e. a preamble, directly into
the picture files and make the main document ignore this code sections.
The package standalone can be used in the main document to skip all extra
preambles in included files. The main file must load all packages and settings
required by the sub-files. Several package options are provided to collect the
preambles of the sub-files automatically and execute them from the main file.
A standalone class is also provided to minimise the extra preamble code
needed in this files. It’s usage is optional, but simplifies and standardises how
picture files are compiled standalone. The class uses by default the crop option
to create an output file which only contains the picture with no extra margins,
page numbers or anything else. A configuration file standalone.cfg read by
the class allows the user to adjust settings and macros easily on a per directory
base.
5
4.2 Version update and backwards compatibility
The default behaviour of v1.x of the standalone class is slightly different as the
one of v0.x, but should result in the same output for the majority of standalone
files. In previous versions the preview option was enabled by default, but since
v1.0 the new, similar crop option is now used. This change should improve
several use-cases, like avoiding the creation of a paragraph due to a trailing
empty line and issues with TikZ patterns under XELATEX. However, paragraph
breaks are now ignored by default, which should be no issue at all for picture
and similar environments which are the main target of the standalone class.
Additionally, the default border has been changed from the preview default of
0.50001bp to no border (0pt). Both of these settings can be changed back to the
old default by adding \standaloneconfig{preview,border=0.50001bp} in
the configuration file or explicitly stating these options as class options.
One true incompatibility between v0.x and v1.x is the load point of the class
configuration file. In v0.x the configuration file was loaded after all options where
processed in order to have all if-switches their final value. In v1.x the configura-
tion file is now loaded directly before the given class options are processed. This
allows to easily set default options for all standalone files. Code which relies on
if-switches (like \ifstandalone and \ifstandalonebeamer) should be placed
inside a \AtEndOfClass{〈code〉} macro. This change might require an update
of personal configuration files.
6
while external writes them from the main file to temporary external files. The
user must decide which workflow is better suited for him/her. Also standalone
is working independently of tikz and supports other picture environments like
pstricks or any other TEX material.
7
5 Usage of the standalone class
5.1 Basic usage
Creating a basic standalone is straight-forward: Create a normal LATEX document
which uses the standalone as document class. The preamble should load all
required packages and libraries for the content. The content, usually a single
picture environment like tikzpicture, is placed in the document body. Empty
lines before and after the picture should be avoided. Also the \begin{document}
and \end{document} should each stand on a source line of their own.
Listing 1: Basic use of the standalone class.
\documentclass{standalone}
\usepackage{somepackage}
\begin{document}
\begin{somepicture}
\somedrawingcommands
\end{somepicture}
\end{document}
Such a file can be compiled as normal. The standalone class will crop the
resulting output file (PDF or DVI/PS) to the content size plus a certain border.
Page number and other header or footer material will be suppressed.
For pictures drawn with TikZ a dedicated tikz option is provided which loads
the tikz package and also configures the tikzpicture environment to create a
single cropped page. For PSTricks pictures an corresponding pstricks option is
provided.
Listing 2: Basic use of the standalone class.
\documentclass[tikz]{standalone}
%\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (2,1) node [midway] {Example};
\end{tikzpicture}
% Further ’tikzpicture’ environments are possible which will create further pages.
\end{document}
8
All boolean options take either ‘true’ or ‘false’ as optional values. Oth-
erwise, if the option is used without a value, ‘true’ is used. If not mentioned
otherwise all options set to ‘false’ initially. Options might switch other options
on or off. For example, mutual exclusive options will disable each other. The
order of the option is obeyed and later options will prevail over earlier ones.
By default the crop option with border=0 is enabled. In versions prior to v1.0
the option preview was the default. This chance was deemed required and should
not affect most documents. However, in some cases resetting the preview option
might be required.
Certain class options can also be set inside the preamble or document body
using \standaloneconfig{〈options〉}.
class=〈class name〉
Specifies the underlying class which is loaded by the standalone class. By de-
fault article is used, which should be suitable for standalone pictures. In
certain cases it may be from benefit to use the same class than in the targeted
main document. For the beamer class the special beamer option should be used
instead.
crop=true|false
If enabled this option crops the content to its natural size plus a specified border.
This is done by saving the content in a box register and resizing the page size
relative to the box dimensions. This option is enabled by default (since v1.0). This
option is mutually exclusive with the similar preview option and will therefore
disable it. If both options are used the last one will be enabled and the other will
be disabled. Also float=false will be set by crop=true in order to avoid issues
with floating environments.
preview=true|false
If enabled this option loads the preview package with the tightpage option and
wraps the content into a preview environment. This crops the content to its
natural size plus a specified border. Issues with the preview options and TikZ
shadings under XELATEXhave been reported. In this cases the crop option should
be used instead. Note that this option was enabled by default for versions before
v1.0, but since then crop is enabled by default.
This option is mutually exclusive with the similar crop option and will there-
fore disable it. If both options are used the last one will be enabled and the
other will be disabled. Also float=false will be set by preview=true in order
to avoid issues with floating environments.
9
border=〈length (all sides)〉
border={〈length (left/right)〉 〈length (bottom/top)〉}
border={〈length (left)〉 〈length (bottom)〉 〈length (right)〉 〈length (top)〉}
This option allows to specify the border used by the preview and crop options. An
alternative name of this option is margin. The border can either be given using a
single value for all sides, separately for the horizontal and vertical borders or for
all sides separately. Multiple values are separated by spaces, which require the
whole value to be wrapped in braces. By default a border of 0pt is set.
This option can be changed during the document using \standaloneconfig
and will affect all following pages.
multi=true|false
multi={〈environment name〉, ...>}
By default the standalone class assume that the whole content is one block
which should be shown on one single page. If this option is activated multi-
ple pages are supported. Each page will be cropped to its content plus the se-
lected border (as long either preview or crop are enabled). A set of environments
which hold a single page must either be given as option value or declared using
\standaloneenv{〈environment name〉, ...}. No typeset material should be
used outside such environments. Note that this option is enabled automatically
by \standaloneenv if either crop or preview is enabled. However, it needs to be
set explicitly as class option if the ignorerest option is also set. If environment
names are provided as option values the option is set to ‘true’ and the envi-
ronments are passed to \standaloneenv which is executed at the begin of the
document environment, where all mentioned environments should be already
defined.
ignorerest=true|false
This option is only meaningful when both multi and crop are enabled. Then it
determines if all material which does not appear inside environments declared
with \standalone should be ignored or not. This is done by boxing and discard-
ing all outside material. Any code will be placed inside a group and therefore
local settings made between environments will not affect later code. Code in the
preamble is not affected. It is recommended to keep this option disabled and
only use it if really required. It should be noted that which preview such material
is always ignored while not affecting local settings. Therefore the ignorrest option
can be seen as a compatibility setting to make crop act more like preview, if this is
required by the user.
10
multido=true|false
Often the multido package with its \multido macro is used to produce several
iterations of a diagram. Usually every iteration should be placed on a seperate
page. This option simplifies this task be changing \multido to automatically
wrap the content (3rd argument) in a multi environment. This option will only
work if the \multido macro is on the top level and not part of an environment
like pspicture.
The ignorerest option is supported by multido because special care is taken to
not ignore the \multido macros.
The nesting of \multido macros is supported and only the outer one will
produce pages while the inner ones are acting normally. The same is true for
\multido inside any multi environment.
This option sets the option multi=samultido, loads the multido package
and redefines its internal macro \multido@. This is done to also support the
other macros \mmultido, \Multido and \MMultido.
varwidth=true|false
varwidth=〈width〉
A trailing empty line between the content and \end{document} will normally
create a paragraph which is \linewidth wide. This paragraph (or any other one)
will enlarge the size of smaller pictures and display itself as a large right border.
This option uses the varwidth package to wrap the content into a varwidth
environment, which is based on minipage, but will always use the natural width
of the content if it is smaller than the given maximum width. The resulting effect is
that the created paragraph will not cause any additional width and that multiple
paragraphs can be included as part of the content. The used maximal width
(which is provided to the underlying minipage environment) is \linewidth by
default, but can be set by provided a width as value to the option. Doing so will
also switch the option on.
If the crop option is used the content is placed in restricted horizontal mode
which ignores paragraph breaks. Using the varwidth option paragraph breaks are
enabled again.
A drawback of this option is that the content will be limited to the given width,
i.e. wider picture environment will be cropped to the width at the right side. In
such cases either a larger width should be selected, the option be switch off,
any paragraph breaks should be avoided (no trailing empty lines) or one of the
specific picture options like tikz or pstricks should be used instead.
This option can be changed during the document using \standaloneconfig
and will affect all content of the following pages.
11
tikz=true|false
This option declares that the content contains of one or more tikzpicture
environments. This sets multi=tikzpicture,varwidth=false and loads the
tikz package.
pstricks=true|false
This option declares that the content contains of one or more pspicture or
pspicture* environments. This sets multi=pspicture,varwidth=false and
loads the pstricks package. Because pspicture* uses pspicture internally it
is also supported. Other environments which use it as well should also be sup-
ported, but might also declared explicitly using \standaloneenv{〈environment
name〉, ...}.
beamer=true|false
If set to ‘true’ this option enables a special beamer mode, where the normal
cropping is disabled. Instead the content is shown on a blank beamer frame.
float=true|false
If this option is that to ‘false’ (which is the default) any floats like figure and
table environments are turned into non-floating environment. This is required
for the options crop and preview to work, so these will set float=false when
set to ‘true’ itself. In general it is recommended to keep floating environments
inside the main document and only place the content of them into standalone
files. This also makes it simple to include the same content in different floats of
different main documents.
If custom floats are defined using a package like float are not supported
yet. Dependent on the way they define floats they might still work. For these
float=true should be set as class options so that the normal definition of floats
is preserved. Afterwards \standaloneconfig{float=false} can be used to
disable floats while taking the changed float definition into account.
convert={〈conversion options〉}
png={〈conversion options〉}
These options allow to enable and configure the conversion feature. See sec-
tion 5.6 for the full description.
12
both the class and package and can be used in standalone files but also in the
main document.
\standaloneconfig{〈options〉}
This configuration macro accepts the class options described in section 5.2. It
can be used inside the class configuration file to set default settings used by all
standalone files, as mention in section 5.5. These settings are set just before the
class options of the standalone file are processed.
Certain class options (e.g. border, varwidth) which do not have a global effect
can also be changed using this macro later in the preamble or even inside the
document body between different content if the multi option is enabled.
\standaloneenv{〈environment〉,〈environment〉,...}
If the multi option is in effect this macro should be used to declare all environ-
ments which produce content. Common examples of such environments are
tikzpicture, pspicture and other picture environments. This macro must
only be used inside the preamble. Every use of such an environment in the docu-
ment body will produce a new page. An exception are nested appearances of such
environments, e.g. a tikzpicture inside a node of another tikzpicture. The
environments must be previously defined and must not be redefined afterwards.
Multiple appearances of the same environment name inside one or multiple
\standalonenv should be avoided.
This macro uses \PreviewEnvironment internally if the preview option is
active. Own code is used with the alternative crop option. If none of these options
are enabled this macro will have not effect and will be silently ignored.
\standaloneignore
In rare cases some code must be placed before the \documentclass of a sub-
file (e.g. \PassOptionsToPackage). Because the main document will only skip
code between \documentclass and \begin{document} this code will be ex-
ecuted by it. In order to avoid this the macro \standaloneignore can be
used at the very beginning of a sub-file to skip over this code. However it must
be written as \csname standaloneignore\endcsname to avoid a ‘Undefined
control sequence’ error when compiled standalone. After all the class is not
loaded at this point, therefore no standalone macros are yet defined. The
\csname . . . \endcsname construct will simple make it equal to \relax in this
case.
Please note that all code before \documentclass is not processed by any of
the subpreamble options but always simply removed. This macro was inspired by
the similar macro \docmute of the docmute package.
13
\begin{standalone}
〈sub-file content〉
\end{standalone}
\ifstandalonebeamer
Both the class and the package provide the if-switch \ifstandalonebeamer,
which can be used to only include code if the file is compiled standalone with the
beamer class option set. The switch is set to \iftrue by the class when loaded
with the beamer option and always to \iffalse by the package. It can be used to
place beamer specific options in the configuration files, which should be skipped
for non-beamer standalone files. If used inside the configuration file this switch
must be placed inside \AtEndOfClass{...}, because the beamer option is not
yet processed
14
\begin{standaloneframe}<〈overlay specification〉>[<〈default overlay spec〉>]
[〈options〉]{〈optional frame title〉}{〈optional frame subtitle〉}
〈code with beamer overlays〉
\end{standaloneframe}
15
Listing 5: Effective code if included in a beamer presentation.
\begingroup
\only<1>{ One }%
\only<2>{ Two }%
\endgroup
\endinput
16
used to modify the conversion command directly. Note that macros used inside
documentclass options must be protected from expansion. This can either be
done wrapping the whole argument in an \unexpanded{..}(requires e-TEX)
or by using \noexpand in front of any macro.
The conversion process is currently implemented in the following way to allow the
normal compilation and subsequent conversion using only one (manual) com-
piler run. Because the document must be fully compiled before the conversion
1 Maybe named differently depending on the used LAT X distribution
E
17
Table 1: Conversion Options (to be used in the value of convert class option)
18
can occur the standalone executes the same LATEX compiler (e.g. textttpdflatex)
again as a sub-process which compiles the current document fully. This is done
when the standalone class is loaded, so that the main compiler instance is still
at \documentclass and has not yet itself opened the output file for writing. Af-
ter the document got compiled using the sub-process the external conversion
tool will be executed. If required intermediate conversions like dvips are also
executed beforehand. Finally the main compiler run is terminated without pro-
ducing any output, keeping the output file generated by the sub-process intact. A
drawback of this implementation is that the log file created by the sub-process is
overwritten by the main process and does not hold meaningful information. This
can be compensated by setting a different jobname for the sub-process using the
subjobname conversion setting.
19
5.8 FAQ / Troubleshooting
This section expands some issues and their solution which can arise with the
standalone class.
A large white space / border on the right side occurs when the content is placed
inside a paragraph. This causes the content to be \linewidth wide and so
smaller pictures will contain now a white space at the right. A common cause for
this is that there was is a empty line between the content and \end{document}
which causes a paragraph break.
This issue can be solved by either removing any trailing lines or other para-
graph breaks, or by using the varwidth option which suppresses the extra added
width. It is also possible to use the multi option and \standaloneenv{〈environment
name〉} to declare certain environments as page content. The tikz option does
this for tikzpictures and the pstricks option for pspicture. See the descrip-
tions of these options for more details.
If the content is cropped to much on the right side, check if the varwidth option
is used. In this case the used maximum width (\linewidth by default) is too
small. A larger width can be set using varwidth=〈length〉 or the option can be
disabled altogether using varwidth=false. The largest width possible is given by
\maxdimen, which however might cause internal overflows.
This can also be caused with beamer content (i.e. when the beamer option
is used). In this case no cropping or varwidth environment is used at all, but
the content is simply to large to fit on a beamer frame. To avoid this rescale
the content to do fit. This can be realised by either using scaling facilities of the
used picture environment (like scale with environment, but this only scales
coordinates) or using \scalebox or \resizebox from graphicx. For compli-
cated code which contains verbatim or other catcode changing code either the
\Resizebox from the realboxes package or the {adjustbox}{scale=<factor>}
environment from the adjustbox package should be used.
This is caused while multi=true and crop=true are set but ignorerest=false
and the document contains typeset material outside of environments declared
with \standaloneenv. To avoid that this extra material should be removed or
ignorerest should be set to true. This will also ignore all settings inside the
document body which are not inside a declared environment. These can be
moved to the preamble instead. See the description of the ignorerest option for
more details.
20
In a multi-page document using DVI/PS mode all pages except the first have a
vertical offset
The vertical reference points in PostScript could does not change when the pages
are resized to fit the individual content of every page. Therefore an offset is
added to compensate for this, which shifts the content to the appropriate vertical
position. Should this not work as expected please inform the package author
and provide a small example which causes this issue, together with the version
number of the used latex compiler and tools (like dvips, ps2pdf) as well as the
used standalone bundle.
The crop option uses PostScript commans in DVI mode, i.e. when latex not
pdflatex (or others) is used as a compiler. This PostScript commands will only
work once the DVI is converted to PS or EPS. Currently this cropping code is
experimental and might not produce a full (E)PS standard compatible file. This
can lead to wrong bounding boxes and wrong orientations or, dependent on
the used PostScript tool, even to PostScript compiler errors. Some issues can
be overcome by converting the the (E)PS file to a (more) standard compatible
version using tools like eps2eps or Ghostscript.
Floating environments like figure or table can not be used while float=true
and either crop=true or preview=true is set. The last two options will try to
store the float into a box which is not allowed (because it can’t the float any longer).
Usually float=false will solve this error, because it turns these environments
into non-floating alternatives. Because both the crop and preview option will
set float=false themselves, this issue can only arise when the float option is
manually set afterwards.
In order for the image conversion to work an external conversion software must
be installed. By default either Image Magkick or GhostScript is used. Please
insure that either or both of these softwares are installed. Installation guide
for your operating system should be easily available on the Internet. The LATEX
compiler option -shell-escape must be used to allow this external software
to be executed from within the LATEX code. If this two points are fulfilled but the
conversion does still not work, please check the log file. The lines in question
start with ‘runsystem’ (at least with TEX Live 2011).
21
6 Usage of the standalone package
6.1 Basic usage
The standalone package needs simply be loaded using \usepackage in a main
document. It redefines the \documentclass macro, which can occur in sub-files,
so that it ignores anything till the next \begin{document} and then takes the
document environment as a simple group. The real document environment in
the main file is not affected. Sub-files can then be included in the main document
body using \input{〈filename〉}.
The standalone package must not be loaded before the document class
using \RequirePackage, because this will cause issues. Also it is not possible to
\input standalone files inside the preamble, e.g. as part of a \savebox assign-
ment.
It is possible to cascade standalone files, i.e. \input a standalone file from
within a standalone file. Then both the standalone class and the standalone
package must be loaded by the any parent standalone file. These parent files can
still be used inside other LATEX documents if these load the standalone package
themselves.
See section 6.2 for a list of package options which enable further features.
subpreambles=true|false
22
sort=true|false
With only the subpreambles option set, the sub-preambles are simple read and
executed unchanged. This includes the risk of option clashes if one package is
loaded with different options inside the sub-preambles and/or the main pream-
ble. This is avoided by the sort option, which accumulates all packages loaded
by all sub-files together with their options. The options are then marked to be
loaded by the package using LATEXs \PassOptionsToPackage macro. The pack-
ages are loaded at the end of the preamble using the \AtBeginDocument hook.
This allows the user to load the same packages with own options in the main file,
after the standalone package is loaded, without any option clashes.
print=true|false
While the sort option is giving already good results, problems with the order of
packages can still occur. Some packages provide, redefine or patch the same
macros, so that they must be loaded in the correct order to give the desired result.
Potential additional code in the sub-preambles, required for some sub-figures
but maybe incompatible with others, complicates the situation further. If such
issues occur they can hardly be handled in an automatic way. Instead the sub-
preambles must be carefully merged into the main preamble. The option print
was created to simplify this otherwise cumbersome task. It concatenated all sub-
preambles into a single file named ‘〈main tex file name〉.stp’ (for standalone,
print). Each preamble is commented with its original file name. Please note
that .sta file mentioned above, while quite similar, holds additional macros and
might not be easily user readable or editable. After the file was generated it can
be easily pasted into the main file preamble using a text editor.
When the print option is enabled the normal .sta file is not generated or
loaded. Because this will cause most likely some errors related to packages not
loaded, all sub-file bodies will be skipped. A warning is printed for each sub-file
to remind the user about this fact. The print option is only indented to by used
when required to get a list of sub-preambles. After including this list in the main
file the option must be removed to compile the main file normally.
print,sort
Finally if both the print and sort options are enabled, a ‘sorted’ list of sub-preambles
is printed into the .stp file. In this ‘sorted print’ mode all \usepackage macros
and other similar macros like \usepgflibrary, \usetikzlibrary as well as
\usetikztiminglibrary from the pgf, tikz and tikz-timing packages, re-
spectively) are removed from the rest of the sub-preamble code. A list of packages
(and libraries) without duplicates is printed at the begin of the .stp file (using
\usepackage, of course). Every option provided by any sub-file for a package
is added, again without duplicates. If specific package date was requested in
23
a sub-file it is also added. If multiple dates are requested for one package, the
most recent (i.e. the “highest one”, not the last processed) is used. After this list(s)
the rest of the sub-preamble code is printed with the above macros removed.
This mode frees the user from the need to remove duplicates and collect package
options manually.
Please note that all \usepackage and similar macros inside braces {} will not
be seen by standalones sort macro and therefore are not extracted or handled
in any special form mentioned above. This can be exploited to load certain pack-
ages only in standalone mode but not in the main document. Unfortunately,
macros inside \ifstandalone...\fi are seen and extracted while not wanted
inside the main file. The macro \onlyifstandalone{〈code〉}(see below) was
created because of this two reasons. Its argument braces hide the content from
the scanner. It is then also completely removed from the printed sub-preamble
code.
comments=true|false
nocomments
The comment option selects if the .stp file should also include the comments
of the sub-preambles. For backwards compatibility nocomments exists which
is identical to comments=false. Comments are included by default in the non-
sorting print mode (print without sort option), but can cause ‘wrong’ results
during the ‘sorting’ process and are therefore removed by default in this mode.
The reason for this can be explained as follows. In order to transfer the comments
from the sub-files to the .stp file TEX must be instructed to handle them as
normal input and not discard them. However, in this case the scanning algorithm
which removes \usepackage and friends can not distinguish between ‘active’
macros and macros which are commented out. All above mentioned macro
inside comments will then be processed as when there where ‘active’. The user
might favour the information provided by the comments over this small risk and
enable them using the comments option.
group=true|false
This option is set the ‘true’ by default and controlled whether or not a group is
added around the content of standalone files. Normally (‘true’) the document
environment of the sub-files is turned into an environment which does nothing,
besides adding the usual group. If set to ‘false’ this environment made trans-
parent, so that no group is added. Any definition inside the document body of
sub-files will still be accessible after the \input macro. Note that this does not
effects the \includestandalone macro which always will add a group.
24
mode=〈mode〉
Sets the mode for \includestandalone. Valid values are ‘tex’ (use source file,
default), ‘image’ (use existing image file produced by the source file), ‘image|tex’
(use image if available, source otherwise), ‘build’ (build image from source, then
use it), ‘buildmissing’ (only build image if it does not exist) and ‘buildnew’
(only build image if source file is newer; doesn’t work with XELATEX). See section 6.3
for more details. See also section 6.4 for further details.
obeyclassoptions=true|false
If this option is enabled the \includestandalone will try to obey the class
options used in the standalone files while in ‘tex’ mode. This only works if the
standalone file uses the standalone class and only with certain options. The
class configuration file will also be loaded (in a local scope, for every standalone
file) in order to load the default settings.
This feature is intended to ensure (nearly) identical results independent if
the standalone files are included as source code or as image, in order to per-
mit an easy switch between this two modes. In particular, the standard size
options 10pt, 11pt and 12pt are applied to the standalone file (supported for
the standard and KOMA Script classes) as well as the border class option. The
multi’=’<environment>, . . . option is supported and will make the page=〈number〉
option of \includegraphics work with \includestandalone. This means,
that one particular page can be selected, while all other environments are skipped.
By default the first page is taken (if multi was used). The special value of -1 will
include all pages from the source file (but not from the image). Because multi op-
tion will assume that either crop or preview is enabled and will always ignore other
content like with ignorerest=true. These three class option will be ignored by
the package, which might lead to different behaviour between standalone and
main-document mode, but only for uncommon cases where multi is used without
declaring environments and with disabled cropping (crop/preview). In order to
support a potential varwidth option the varwidth is loaded if it is available.
This is an extended feature, which requires substantial amount of extra code
and some advanced techniques to switch the font size. It might not work correctly
under all circumstances. Because of this it is disabled by default. At the moment
it does not take the class configuration file into account and does not work for
beamer standalone files.
extension=〈.extension〉
The image file extension (with leading dot) used for mode=image can be selected
using this option. By default the target output file extension of the used LATEX
compiler is used, i.e. ‘.pdf’ for pdflatex, lualatex and xelatex and ‘.eps’
(converted from DVI) for latex.
25
build={〈build options〉}
This option allows to set the options used for building images from standalone
files. See section 6.4, especially Table 3 for further details.
6.3 Macros
The following user macros are provided by the standalone package. Further
macros are listed in section 7 which are defined by both the class and package
and can be used in standalone files but also in the main document.
\standaloneconfig{〈options〉}
\includestandalone[〈options〉]{〈file〉}
26
from given standalone source files. This is controlled by the mode options. This
was already described in section 6.2 and 6.3.
This enables the user to switch easily between including source code or
images, either globally or only for selected standalone files. Using images has
the benefit that the included material, often complicated pictures, does not have
to be recompiled every time with the main document. This leads to significant
speed improvements. The drawback is a slight increase in file size, because
the material will have its own file headers. Also any settings done in the main
document which would affects the source code will not have an effect on the
image. This can be positive or negative dependent on the case.
An extended feature is the automatic building of images from the standalone
files, either always or only if the source files are newer than the existing image
files. In this cases the \includestandlone macro will call the LATEX compiler on
the standalone files in question to produce the images, then include these using
\includegraphics. This requires the ‘-shell-escape’ compiler option to be
set, otherwise the execution of shell commands is disabled for security reasons.
The image files will normally be created in the current directory of the main
document, which is not necessarily the same directory where the source files are
located. Dependent on the used compiler settings, files in the current directory
will be found first before other directories are searched. Using mode=buildnew
newly build image files placed in the current directory will therefore taken before
older images files potentially located in the directory of the standalone files. Be-
cause the exact directory of source files is not accessible within LATEX documents,
it is not possible to create the images files always in the same directories as the
source files. Compiler options like ‘-output-directory’ can be useful to influ-
ence the output directory of the build images. However, these options must be
used with the internal compiler run, i.e. by setting build={latexoptions=〈...〉}
appropriately, not (only) on the main LATEX compiler run.
If the build process fails a warning is issued and the source code will be
included instead. It should be noted that failure detection is not perfect and
might lead to false positives or negatives.
7 Common macros
The following conditional macros are defined by both the standalone class and
package, but react differently when the code is compiled standalone or as part of
a main document.
\ifstandalone
Both the class and the package provide the if-switch \ifstandalone, which can
be used to only include code if the file is compiled standalone. The switch is set
to \iftrue by the class and to \iffalse by the package.
27
Table 3: Build settings
This is the macro version of the \ifstandalone if-switch. It executes the first
argument only in standalone mode, i.e. when the file is compiled on its own.
When included in the main document the second argument is executed instead.
As mentioned in section 6.2 it can also be used to hide \usepackage and similar
macros from the extraction scanner of the sort option. The macro and its
arguments is not printed into the .stp file.
\onlyifstandalone{〈code〉}
This macro is similar to \IfStandalone but only has takes one argument which
is executed only in standalone mode, but ignored when compiled as part of a main
document. As mentioned in section 6.2 it can also be used to hide \usepackage
and similar macros from the extraction scanner of the sort option. The macro
and its argument is not printed into the .stp file.
28
8 Usage Examples
29
Example 3: Effective code if compiled standalone.
\documentclass{article}
\newenvironment{standalone}{\begin{preview}}{\end{preview}}
\input{standalone.cfg}
% which by defaults loads:
% \PassOptionsToPackage{active,tightpage}{preview}
\usepackage{preview}
\usepackage{tikz}
\begin{document}
\begin{standalone}
\begin{tikzpicture}
\draw (0,0) rectangle (2,1) node [midway] {Example};
\end{tikzpicture}
\end{standalone}
\end{document}
30