Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to add TeX libraries and add to the TeX preamble in the PGtikz.pl macro #541

Merged
merged 1 commit into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/TikZImage.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ sub new {
tex => '',
tikzOptions => '',
tikzLibraries => '',
texPackages => {},
addToPreamble => '',
ext => 'png',
imageName => ''
};
Expand Down Expand Up @@ -76,6 +78,19 @@ sub tikzLibraries {
return &$self('tikzLibraries', @_);
}

# Set additional TeX packages to load. This accepts a single hash parameter.
sub texPackages {
my $self = shift;
return &$self('texPackages', $_[0]) if ref($_[0]) eq "HASH";
return &$self('texPackages');
}

# Additional TeX commands to add to the TeX preamble
sub addToPreamble {
my $self = shift;
return &$self('addToPreamble', @_);
}

# Set the image type. The valid types are 'png', 'gif', 'svg', and 'pdf'.
# The 'pdf' option should be set for print.
sub ext {
Expand All @@ -93,8 +108,13 @@ sub header {
my $self = shift;
my @output = ();
push(@output, "\\documentclass{standalone}\n");
push(@output, "\\usepackage[svgnames]{xcolor}\n");
push(@output, "\\usepackage{tikz}\n");
push(@output, map {
"\\usepackage" . ($self->texPackages->{$_} ne "" ? "[$self->texPackages->{$_}]" : "") . "{$_}\n"
} keys %{$self->texPackages});
push(@output, "\\usetikzlibrary{" . $self->tikzLibraries . "}") if ($self->tikzLibraries ne "");
push(@output, $self->addToPreamble);
push(@output, "\\begin{document}\n");
push(@output, "\\begin{tikzpicture}");
push(@output, "[" . $self->tikzOptions . "]") if ($self->tikzOptions ne "");
Expand Down
57 changes: 38 additions & 19 deletions macros/PGtikz.pl
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,58 @@ =head1 DESCRIPTION
This is a convenience macro for utilizing the TikZImage object to insert TikZ
images into problems. Create a TikZ image as follows:

$image = createTikZImage();
$image->tex(<<END_TIKZ);
\draw (-2,0) -- (2,0);
\draw (0,-2) -- (0,2);
\draw (0,0) circle[radius=1.5];
END_TIKZ
$image = createTikZImage();
$image->tex(<<END_TIKZ);
\draw (-2,0) -- (2,0);
\draw (0,-2) -- (0,2);
\draw (0,0) circle[radius=1.5];
END_TIKZ

Then insert the image into the problem with

image(insertGraph($image));
image(insertGraph($image));

=head1 DETAILED USAGE

There are several TikZImage parameters that may need to be set for the
TikZImage object return by createTikZImage to generate the desired image.

$image->tex() Add the tikz commands that define the image.
This takes a single string parameter. It is
generally best to use single quotes around the
string. Escaping of special characters may be
$image->tex() Add the tikz commands that define the image.
This takes a single string parameter. It is
generally best to use single quotes around the
string. Escaping of special characters may be
needed in some cases.

$image->tikzOptions() Add options that will be passed to
\begin{tikzpicture}. This takes a single
$image->tikzOptions() Add options that will be passed to
\begin{tikzpicture}. This takes a single
string parameter.
For example:
$image->tikzOptions(
"x=.5cm,y=.5cm,declare function={f(\x)=sqrt(\x);}"
);

$image->tikzLibraries() Add additional tikz libraries to load. This
$image->tikzLibraries() Add additional tikz libraries to load. This
takes a single string parameter.

$image->ext() Set the file type to be used for the image.
The valid image types are 'png', 'gif', 'svg',
and 'pdf'. The default is a 'png' image. This
macro sets this to 'pdf' when a hardcopy is
For example:
$image->tikzLibraries("arrows.meta,calc");

$image->texPackages() Add tex packages to load. This takes a hash for
its parameter. The keys of this hash are the
package names, and the corresponding values a
string consisting of options for the package.
For example:
$image->texPackages({
"pgfplots" => "",
"hf-tikz" => "customcolors"
});

$image->addToPreamble() Additional commands to add to the TeX preamble.
This takes a single string parameter.

$image->ext() Set the file type to be used for the image.
The valid image types are 'png', 'gif', 'svg',
and 'pdf'. The default is a 'png' image. This
macro sets this to 'pdf' when a hardcopy is
generated.

=cut
Expand Down