KristofferC / PGFPlotsX.jl

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

Can not use lower indices in options #310

Closed fangrh closed 1 year ago

fangrh commented 1 year ago

I want to draw 3D TikzPicture with PGFPlotsX.jl like this examplehttps://texample.net/tikz/examples/cone/. And I need use the “tdplot_main_coords” option in tickpicture. The code is

using PGFPlotsX
push!(PGFPlotsX.CUSTOM_PREAMBLE, raw"\usepackage{tikz-3dplot}") # use the 3dplot library
td = TikzDocument()                                                                                # create Tikz document to wrap TikzPicture
push!(td, raw"\tdplotsetmaincoords{70}{0}")                                           # The view direction
tp = @pgf TikzPicture({"tdplot_main_coords", "scale"=>1.0})                #create TikzPicture
s = raw"""
    \def\RI{2}
    \def\RII{1.25}

    \draw[thick] (\RI,0)
    \foreach \x in {0,300,240,180} { --  (\x:\RI) node at (\x:\RI) (R1-\x) {} };
    \draw[dashed,thick] (R1-0.center)
    \foreach \x in {60,120,180} { --  (\x:\RI) node at (\x:\RI) (R1-\x) {} };
    \path[fill=gray!30] (\RI,0)
    \foreach \x in {0,60,120,180,240,300} { --  (\x:\RI)};

    \begin{scope}[yshift=2cm]
    \draw[thick,fill=gray!30,opacity=0.2] (\RII,0)
    \foreach \x in {0,60,120,180,240,300,360}
        { --  (\x:\RII) node at (\x:\RII) (R2-\x) {}};
    \end{scope}

    \foreach \x in {0,180,240,300} { \draw (R1-\x.center)--(R2-\x.center); };
    \foreach \x in {60,120} { \draw[dashed] (R1-\x.center)--(R2-\x.center); };
    """
print_tex(td)

The result is

\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{tikz-3dplot}
\begin{document}
\tdplotsetmaincoords{0}{0}
\begin{tikzpicture}[tdplot main coords, scale={1.0}]
\def\RI{2}
\def\RII{1.25}

\draw[thick] (\RI,0)
\foreach \x in {0,300,240,180} { --  (\x:\RI) node at (\x:\RI) (R1-\x) {} };
\draw[dashed,thick] (R1-0.center)
\foreach \x in {60,120,180} { --  (\x:\RI) node at (\x:\RI) (R1-\x) {} };
\path[fill=gray!30] (\RI,0)
\foreach \x in {0,60,120,180,240,300} { --  (\x:\RI)};

\begin{scope}[yshift=2cm]
\draw[thick,fill=gray!30,opacity=0.2] (\RII,0)
\foreach \x in {0,60,120,180,240,300,360}
    { --  (\x:\RII) node at (\x:\RII) (R2-\x) {}};
\end{scope}

\foreach \x in {0,180,240,300} { \draw (R1-\x.center)--(R2-\x.center); };
\foreach \x in {60,120} { \draw[dashed] (R1-\x.center)--(R2-\x.center); };

\end{tikzpicture}
\end{document}

The "tdplot_main_coords" changes to "tdplot main coords". My question is how to let "tdplot_main_coords" shows just as it's original form?

tpapp commented 1 year ago

Currently you can't, as we replace _s with spaces in option names. See this line at the printing stage. So you can' t even construct options manually.

We could easily add a workaround for this, but I am not sure what the right API is:

  1. a__b
  2. a\_b
  3. nothing, just resolve this when the macro ingests values, and the user can build options with any desired key by pushing to the Dict
fangrh commented 1 year ago

I vote for second one, or \lowerindices. That looks like a native latex code.

KristofferC commented 1 year ago

We should stop doing the _ replacement in strings though? I thought that should only be done for literal symbols. Feels like a bug to me at least. I have some vague recollection of looking at this before...