Skip to content

Commit

Permalink
Merge pull request #7 from jleugeri/master
Browse files Browse the repository at this point in the history
PGFPlotsX Support
  • Loading branch information
mykelk authored May 13, 2020
2 parents 1822b99 + ada1c6a commit 4685709
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ To install this package, just download [juliaplots.sty](https://github.com/sisl/
The package currently supports the following Julia plotting packages:

1. [PGFPlots.jl](https://github.com/sisl/PGFPlots.jl)
2. [PyPlot.jl](https://github.com/stevengj/PyPlot.jl)
3. [Gadfly.jl](https://github.com/dcjones/Gadfly.jl)
2. [PGFPlotsX.jl](https://github.com/KristofferC/PGFPlotsX.jl)
3. [PyPlot.jl](https://github.com/stevengj/PyPlot.jl)
4. [Gadfly.jl](https://github.com/dcjones/Gadfly.jl)

Before using any of these packages, they must be installed and usable from Julia. In the case of Gadfly, you must also have [Cairo.jl](https://github.com/JuliaGraphics/Cairo.jl) installed.

Only one plotting package is permitted. You specify the package you want as a package option to `juliaplots`. For example:
* `\usepackage[pgfplots]{juliaplots}`
* `\usepackage[pgfplotsx]{juliaplots}`
* `\usepackage[pyplot]{juliaplots}`
* `\usepackage[gadfly]{juliaplots}`

Expand All @@ -26,6 +28,20 @@ As with pythontex, you can run code using `jlcode`:
\end{jlcode}
```

For `PGFPlotsX`, the equivalent code block is:
```latex
\begin{jlcode}
x = [1,2,3]
y = [2,4,1]
p = @pgf TikzPicture(
Axis(
PlotInc({ no_marks },
Table(; x = x, y = y))))
plot(p)
\end{jlcode}
```

You can run and typeset code using `jlblock`
```latex
\begin{jlblock}
Expand All @@ -38,7 +54,7 @@ You can run and typeset code using `jlblock`
You can run a command with, for example, `\jlc{x=2}`. You can typeset code without running it with `\jlv{x=2}`. You can run a command and typeset it using `\jlb{x=2}`.

The functionality that is added by juliaplots.jl is the `\plot` command, which will generate the plot and inject it into the document. The number of arguments depends on which plotting package you are using. Here are examples:
* PGFPlots: `\plot{myfile}`, where the sole argument is the name of the file (do not include the file extension) that will be used in the background for including the plot.
* PGFPlots and PGFPlotsX: `\plot{myfile}`, where the sole argument is the name of the file (do not include the file extension) that will be used in the background for including the plot.
* PyPlot: `\plot{myfile}{width=3in}`, where the first argument is the name of the PDF file (without file extension) to store the image, and the second argument are the options to be passed to `\includegraphics` when the PDF is included.
* Gadfly: `\plot{myfile}{3inch}{3inch}{width=3in}`, where the first argument is the name of the PDF file (without file extension) to store the image, the second is the width of the image to be exported by Gadfly, the third is the height of the image to be exported by Gadfly, and the fourth argument contains the options to be passed to `\includegraphics` when the PDF is included.

Expand Down
23 changes: 23 additions & 0 deletions juliaplots.sty
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@
global cur_plot
cur_plot = PGFPlots.plot(args...; kwags...)
end

function plot_pgfplotsx(object, args...; kwags...)
println("test")
global cur_plot
cur_plot = object
if length(args) + length(kwags) > 0
@warn "All additional arguments to plot() ignored! Please pass a TikZDocument or TikZFigure directly."
end
return cur_plot
end

function plot_gadfly(args...; kwags...)
global cur_plot
cur_plot = Gadfly.plot(args...; kwags...)
Expand Down Expand Up @@ -49,6 +60,18 @@
\jlc{plot(args...; kwags...) = plot_pgfplots(args...; kwags...)}
}

\DeclareOption{pgfplotsx}{
\DeclareRobustCommand{\plot}[1]{
\IfFileExists{#1.tex}{}{
\immediate\openoutputfile{#1.tex}{juliaplot}
\immediate\closeoutputstream{juliaplot}
}
\jlc{pgfsave("#1.tex", cur_plot; include_preamble=false)}\input{#1}}
\jlc{using PGFPlotsX}
\jlc{plot(args...; kwags...) = plot_pgfplotsx(args...; kwags...)}
}


\DeclareOption{pyplot}{
\DeclareRobustCommand{\plot}[2]{
\IfFileExists{#1.pdf}{\includegraphics[#2]{#1}}{
Expand Down
Binary file added pgfplotsx_example.pdf
Binary file not shown.
19 changes: 19 additions & 0 deletions pgfplotsx_example.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
\documentclass{article}
\usepackage[pgfplotsx]{juliaplots}

\begin{document}

\begin{jlblock}
x = [1,2,3]
y = [2,4,1]

p = @pgf TikzPicture(
Axis(
PlotInc({ no_marks },
Table(; x = x, y = y))))
plot(p)
\end{jlblock}

\plot{myfile}

\end{document}

0 comments on commit 4685709

Please sign in to comment.