KristofferC / PGFPlotsX.jl

Plots in Julia using the PGFPlots LaTeX package
Other
301 stars 40 forks source link

Add a CUSTOM_AXIS_OPTIONS parameter ? #277

Closed BambOoxX closed 2 years ago

BambOoxX commented 2 years ago

In general, I tend to use a specific style for my figures, to reproduce the axis line styles, grid styles, figure dimensions and others.

When writing the whole code in LaTeX, I use a \pgfplotsset{every axis/.append style={...}} to do that. I tried doing something similar in julia with push!(PGFPlotsX.CUSTOM_PREAMBLE,raw"\pgfplotsset{every axis/.append style={width = 7cm, height = 4cm, scale only axis}}") without success.

I am wondering if it is because such calls are misinterpreted by julia or if we should add a specific way of doing this with a new parameter list such as CUSTOM_AXIS_OPTIONS ?

tpapp commented 2 years ago

Please provide a self-contained MWE.

BambOoxX commented 2 years ago

This is a MWE based on Plots with PGFPlotsX as a backend. If you think using PGFplotX directly should make a difference, I will try and make a specific MWE.

using Plots
pgfplotsx()
# reference plot
plt1 = plot(rand(10)) #<-- gives 1st picture
savefig(plt1,"1.png")
# plot with modified fonts
push!(PGFPlotsX.CUSTOM_PREAMBLE,raw"\usepackage{newtxtext,newtxmath}")
plt2 = plot(rand(10)) # <-- gives 2nd picture (font is correctly changed)
savefig(plt2,"2.png")

# plot with custom dimensions
push!(PGFPlotsX.CUSTOM_PREAMBLE,raw"\pgfplotsset{every axis/.append style={width = 7cm, height = 4cm, scale only axis}}")
plt3 = plot(rand(10)) #<-- gives 3rd picture (but output size is incorrect)
savefig(plt3,"3.png")

Picture 1 1

Picture 2 2

Picture 3 3

tpapp commented 2 years ago

Works fine for me with PGFPlotsX directly:

julia> using PGFPlotsX

julia> push!(PGFPlotsX.CUSTOM_PREAMBLE,raw"\usepackage{newtxtext,newtxmath}")
1-element Vector{String}:
 "\\usepackage{newtxtext,newtxmath}"

julia> p = Plot(Table(1:10, rand(10)))

julia> mktemp() do path, io
       path *= ".tex"
       pgfsave(path, p)
       print(read(path, String))
       end
\RequirePackage{luatex85}
\documentclass[tikz]{standalone}
% Default preamble
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{groupplots}
\usepgfplotslibrary{polar}
\usepgfplotslibrary{smithchart}
\usepgfplotslibrary{statistics}
\usepgfplotslibrary{dateplot}
\usepgfplotslibrary{ternary}
% Custom preamble from global variable:
\usepackage{newtxtext,newtxmath}
\begin{document}
\begin{tikzpicture}
\begin{axis}
    \addplot
        table[row sep={\\}]
        {
            \\
            1.0  0.6793680467215424  \\
            2.0  0.45903377344883056  \\
            3.0  0.6449446620649999  \\
            4.0  0.4054748173598529  \\
            5.0  0.9034463312648656  \\
            6.0  0.4954700932822882  \\
            7.0  0.3087832461557233  \\
            8.0  0.5392974240263115  \\
            9.0  0.858539634174244  \\
            10.0  0.9793512572522536  \\
        }
        ;
\end{axis}
\end{tikzpicture}
\end{document}
BambOoxX commented 2 years ago

Indeed, once again, this seems to be due to Plots rather than PGFPlotsX thanks for the help.