Expand description
Picture environment.
Creating a Picture is equivalent to the TikZ graphics environment:
\begin{tikzpicture}[PictureKeys]
% axis environments
\end{tikzpicture}
Fields§
§axes: Vec<Axis>Implementations§
source§impl Picture
impl Picture
sourcepub fn new() -> Self
pub fn new() -> Self
Create a new, empty picture environment.
Examples
use pgfplots::Picture;
let picture = Picture::new();sourcepub fn add_key(&mut self, key: PictureKey)
pub fn add_key(&mut self, key: PictureKey)
Add a key to control the appearance of the picture. This will overwrite any previous mutually exclusive key.
Examples
use pgfplots::{Picture, PictureKey};
let mut picture = Picture::new();
picture.add_key(PictureKey::Custom(String::from("baseline")));sourcepub fn standalone_string(&self) -> String
pub fn standalone_string(&self) -> String
Return a String with valid LaTeX code that generates a standalone
PDF with the picture environment.
Note
Passing this string directly to e.g. pdflatex will fail to generate a
PDF document. It is usually necessary to str::replace all the
occurrences of \n and \t with white space before sending this string
as an argument to a LaTeX compiler.
Examples
use pgfplots::Picture;
let picture = Picture::new();
assert_eq!(
r#"\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\end{tikzpicture}
\end{document}"#,
picture.standalone_string());sourcepub fn to_pdf<P, S>(
&self,
working_dir: P,
jobname: S,
engine: Engine
) -> Result<PathBuf, CompileError>where
P: AsRef<Path>,
S: AsRef<str>,
pub fn to_pdf<P, S>(
&self,
working_dir: P,
jobname: S,
engine: Engine
) -> Result<PathBuf, CompileError>where
P: AsRef<Path>,
S: AsRef<str>,
Compile the picture environment into a standalone PDF document. This
will create the file jobname.pdf in the specified working_dir
(additional files will be created in the same directory e.g. .log and
.aux files). Return a Result with the path to the generated PDF
file or a CompileError.
Examples
use pgfplots::{Engine, Picture};
let picture = Picture::new();
let pdf_path = picture.to_pdf(std::env::temp_dir(), "jobname", Engine::PdfLatex)?;
assert_eq!(pdf_path, std::env::temp_dir().join("jobname.pdf"));
sourcepub fn show_pdf(&self, engine: Engine) -> Result<(), ShowPdfError>
pub fn show_pdf(&self, engine: Engine) -> Result<(), ShowPdfError>
Show the picture environment in a standalone PDF document. This will
create a file in the location returned by std::env::temp_dir and
open it with the default PDF viewer.
Examples
use pgfplots::{Engine, Picture};
let picture = Picture::new();
picture.show_pdf(Engine::PdfLatex)?;
Examples found in repository?
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
fn main() {
let mut vertices = {
let mut current: Vec<(f64, f64)> = vec![
(0.0, 1.0),
((3.0f64).sqrt() / 2.0, -0.5),
(-(3.0f64).sqrt() / 2.0, -0.5),
];
for _ in 0..5 {
current = snowflake_iter(¤t[..]);
}
current
};
vertices.push(vertices[0]);
// Set snowflake
let mut plot = Plot2D::new();
plot.coordinates = vertices.into_iter().map(|v| v.into()).collect();
plot.add_key(PlotKey::Custom(String::from("fill=gray!20")));
// Customise axis environment
let mut axis = Axis::from(plot);
axis.set_title("Kloch Snowflake");
axis.add_key(AxisKey::Custom(String::from("hide axis")));
#[cfg(feature = "tectonic")]
Picture::from(axis).show_pdf(Engine::Tectonic).unwrap();
#[cfg(not(feature = "tectonic"))]
Picture::from(axis).show_pdf(Engine::PdfLatex).unwrap();
}More examples
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
fn main() {
// Set line
let mut line = Plot2D::new();
line.coordinates = (0..101)
.into_iter()
.map(|i| (f64::from(i), f64::from(i * i)).into())
.collect();
// Set rectangles
let mut rectangles = Plot2D::new();
rectangles.coordinates = (0..101)
.into_iter()
.step_by(10)
.map(|i| (f64::from(i), f64::from(i * i)).into())
.collect();
// Currently have to "guess" the bar width in pt units
// Still pending the \compat key
rectangles.add_key(PlotKey::Type2D(Type2D::YBar {
bar_width: 19.5,
bar_shift: 0.0,
}));
rectangles.add_key(PlotKey::Custom(String::from("fill=gray!20")));
rectangles.add_key(PlotKey::Custom(String::from("draw opacity=0.5")));
// Customise axis environment
let mut axis = Axis::new();
axis.set_title("Rectangle Integration");
axis.set_x_label("$x$");
axis.set_y_label("$y = x^2$");
axis.plots.push(rectangles);
axis.plots.push(line);
axis.add_key(AxisKey::Custom(String::from("axis lines=middle")));
axis.add_key(AxisKey::Custom(String::from("xlabel near ticks")));
axis.add_key(AxisKey::Custom(String::from("ylabel near ticks")));
#[cfg(feature = "tectonic")]
Picture::from(axis).show_pdf(Engine::Tectonic).unwrap();
#[cfg(not(feature = "tectonic"))]
Picture::from(axis).show_pdf(Engine::PdfLatex).unwrap();
}7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
fn main() {
// Set the straight dotted line
let mut line = Plot2D::new();
line.coordinates = (0..11)
.into_iter()
.map(|i| (f64::from(i), 2.0 * PI * f64::from(i)).into())
.collect();
line.add_key(PlotKey::Custom(String::from("dashed")));
// Set the points with error bars
let mut points = Plot2D::new();
points
.coordinates
.push((1.0, 8.0, Some(0.2), Some(0.9)).into());
points
.coordinates
.push((3.0, 16.0, Some(0.4), Some(1.4)).into());
points
.coordinates
.push((5.0, 33.0, Some(0.2), Some(3.4)).into());
points
.coordinates
.push((7.0, 41.0, Some(0.2), Some(3.4)).into());
points
.coordinates
.push((9.0, 58.0, Some(0.5), Some(1.4)).into());
points.add_key(PlotKey::Type2D(Type2D::OnlyMarks));
points.add_key(PlotKey::XError(ErrorCharacter::Absolute));
points.add_key(PlotKey::XErrorDirection(ErrorDirection::Both));
points.add_key(PlotKey::YError(ErrorCharacter::Absolute));
points.add_key(PlotKey::YErrorDirection(ErrorDirection::Both));
points.add_key(PlotKey::Custom(String::from("mark size=1pt")));
// Customize axis environment
let mut axis = Axis::new();
axis.set_title("Slope is $2\\pi$");
axis.set_x_label("Radius~[m]");
axis.set_y_label("Circumference~[m]");
axis.plots.push(line);
axis.plots.push(points);
axis.add_key(AxisKey::Custom(String::from("legend entries={fit,data}")));
axis.add_key(AxisKey::Custom(String::from("legend pos=north west")));
#[cfg(feature = "tectonic")]
Picture::from(axis).show_pdf(Engine::Tectonic).unwrap();
#[cfg(not(feature = "tectonic"))]
Picture::from(axis).show_pdf(Engine::PdfLatex).unwrap();
}