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

Pretty-printing the LaTeX code #138

Merged
merged 6 commits into from
Aug 4, 2020
Merged

Pretty-printing the LaTeX code #138

merged 6 commits into from
Aug 4, 2020

Conversation

mirkobunse
Copy link
Contributor

The biggest advantage of PGFPlots over other plotting packages is that it enables users to alter the LaTeX code. I frequently fine-tune the size, colors, and other properties of a plot to the available space in a scientific paper or a slide set. Almost always, this fine-tuning happens long after the initial plot was generated in Julia.

This PR improves how the LaTeX code is formatted. Namely, the indentation, line breaks, and whitespaces are placed so that a human reader of the code can grasp more easily what is going on. This eases altering the code a lot.

Examples

The following examples are also presented in doc/PGFPlots.ipynb. Here, we will compare the new formatting of some examples with their previous formatting. To the best of my knowledge, there is no change in the final PDF and SVG outputs.

1) Basic plot

x = [1,2,3]
y = [2,4,1]
plot(x, y)
print(tikzCode(x, y))

Before:

\begin{axis}\addplot+ coordinates {
(1, 2)
(2, 4)
(3, 1)
};
\end{axis}

After:

\begin{axis}

\addplot+ coordinates {
  (1, 2)
  (2, 4)
  (3, 1)
};

\end{axis}

2) Basic plot with options

a = Axis(Plots.Linear(x, y, legendentry="My Plot"), xlabel="X", ylabel="Y", title="My Title")
a.ylabelStyle = "rotate = -90"
a.legendStyle = "at={(1.05,1.0)}, anchor=north west"
print(tikzCode(a))

Before:

\begin{axis}[legend style = {{at={(1.05,1.0)},anchor=north west}}, ylabel = {Y}, ylabel style = {rotate = -90}, title = {My Title}, xlabel = {X}]\addplot+ coordinates {
(1, 2)
(2, 4)
(3, 1)
};
\addlegendentry{My Plot}
\end{axis}

After:

\begin{axis}[
  legend style = {at={(1.05,1.0)}, anchor=north west},
  ylabel = {Y},
  ylabel style = {rotate = -90},
  title = {My Title},
  xlabel = {X}
]

\addplot+ coordinates {
  (1, 2)
  (2, 4)
  (3, 1)
};
\addlegendentry{My Plot}

\end{axis}

3) Images

f = (x,y)->x*exp(-x^2-y^2)
p = Plots.Image(f, (-2,2), (-2,2))
print(tikzCode(p))

Before:

\begin{axis}[enlargelimits = false, axis on top, colormap={wb}{gray(0cm)=(0); gray(1cm)=(1)}, colorbar]\addplot [point meta min=-0.42870694108441587, point meta max=0.42870694108441587] graphics [xmin=-2, xmax=2, ymin=-2, ymax=2] {tmp_10000000000001.png};
\end{axis}

After:

\begin{axis}[
  enlargelimits = false,
  axis on top,
  colormap={wb}{gray(0cm)=(0); gray(1cm)=(1)},
  colorbar
]

\addplot[
  point meta min = -0.42870694108441587,
  point meta max = 0.42870694108441587
] graphics[
  xmin = -2,
  xmax = 2,
  ymin = -2,
  ymax = 2
] {tmp_10000000000001.png};

\end{axis}

4) Patches

p = Plots.Patch2D([0   1 1 0     1  2 2 1    1   2 2   1;  # x
                   0   0 1 1     0  0 1 1    1   1 2   2;  # y
                   0.2 0 1 1     0 -1 0 1    0.5 1 0.5 0], # color
                  shader = "interp", patch_type="rectangle")
print(tikzCode(p))

Before:

\begin{axis}\addplot[patch type = {rectangle}, shader = {interp}, patch]
table[point meta=\thisrow{c}] {
	x y c
	 0 0 0.2
	 1 0 0
	 1 1 1
	 0 1 1

	 1 0 0
	 2 0 -1
	 2 1 0
	 1 1 1

	 1 1 0.5
	 2 1 1
	 2 2 0.5
	 1 2 0

};
\end{axis}

After:

\begin{axis}

\addplot[
  patch type = {rectangle},
  shader = {interp},
  patch
] table[
  point meta = \thisrow{c}
] {
  x y c
  0 0 0.2
  1 0 0
  1 1 1
  0 1 1

  1 0 0
  2 0 -1
  2 1 0
  1 1 1

  1 1 0.5
  2 1 1
  2 2 0.5
  1 2 0

};

\end{axis}

@mykelk
Copy link
Member

mykelk commented Aug 3, 2020

Whoa! This is awesome! I'll stress test this over the next couple days on our book and then merge it.

@mykelk mykelk merged commit ef8adf0 into JuliaTeX:master Aug 4, 2020
@mykelk
Copy link
Member

mykelk commented Aug 4, 2020

It works. It's good to go!

@mirkobunse
Copy link
Contributor Author

Wow, thanks for the quick action!

@MaximeBouton
Copy link
Member

This is so cool! many thanks @mirkobunse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants