2

I would like to draw a 3d patch plot in an axis environment with coordinates that have previously been defined.

Here is a dummy example to illustrate what I would like to do:

\begin{figure}
    \begin{tikzpicture}
        \begin{axis}
        \coordinate (a) at (0, 0, 0);
        \coordinate (b) at (1, 0, 0);
        \coordinate (c) at (0, 1, 0);
        \addplot3 [patch,table/row sep=\\,patch table={
        0 1 2\\
        }] table {
        0 0 0\\ % I would like to extract (a) coordinates here instead
        1 0 0\\ % (b) here
        0 1 0\\ % (c) here
        };
        \end{axis}
    \end{tikzpicture}
\end{figure}

The reason for me wanting to do that is that the coordinates I intend to use in the 3d patch plots are defined programmatically in some complicated macros.

Kevlar
  • 154
  • 2
    It can not be done. As soon as you store a coordinate it loses the z component and you need that for the plot. (unless z=0 for all your points?) – hpekristiansen Feb 19 '22 at 03:52
  • 1
    Here is how to do it for 2D: https://tex.stackexchange.com/a/415881/8650 – hpekristiansen Feb 19 '22 at 03:55
  • @hpekristiansen Thank you, it makes sense. In my case I don't have z=0 for all the points (my example could have been better I guess). I'll try to look for a different way of reaching the result I'm looking for. – Kevlar Feb 19 '22 at 04:50

1 Answers1

1

Since it can't be done normally, you might want to consider looking into the sagetex package, documentation here can handle. The package gives you a computer algebra system, called Sage, and access to the Python programming language. The compilation is a 3 step process. The first time LaTeX must run without problems. Next Sage runs. The final step is to put the Sage results/calculations into the document. Since your code depends on coordinates that Sage must use, the output is put into a string. The first pass will be an empty document. The second pass uses Sage to take the coordinates you want. It creates a string which is the code you wanted for your picture. The final step of processing puts the string into the document, which gives the picture. In the code below, %d is a placeholder for an integer; %f is for floating point numbers, %s for strings. The part of the code %(c1[0],c1[1],c1[2]) is telling Sage what goes in the 3 %d arguments before it. The r"" is for raw strings which let us use problematic text, such as \, without problems.

\documentclass[11pt,border=1mm]{article}
\usepackage{sagetex,tikz,pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{sagesilent}
def MyPatch(c1,c2,c3):
    output = r""
    output += r"\begin{figure}"
    output += r"\begin{tikzpicture}"
    output += r"\begin{axis}"
    output += r"\coordinate (a) at (%d, %d, %d);"%(c1[0],c1[1],c1[2])
    output += r"\coordinate (b) at (%d, %d, %d);"%(c2[0],c2[1],c2[2])
    output += r"\coordinate (c) at (%d, %d, %d);"%(c3[0],c3[1],c3[2])
    output += r"\addplot3 [patch,table/row sep=\\,patch table={"
    output += r"0 1 2\\"
    output += r"}] table {"
    output += r"%d %d %d\\"%(c1[0],c1[1],c1[2])
    output += r"%d %d %d\\"%(c2[0],c2[1],c2[2])
    output += r"%d %d %d\\"%(c3[0],c3[1],c3[2])
    output += r"};"
    output += r"\end{axis}"
    output += r"\end{tikzpicture}"
    output += r"\end{figure}"
return output

a1=[0,0,0] a2=[1,0,0] a3=[0,1,0] pic = MyPatch(a1,a2,a3) \end{sagesilent} \sagestr{pic} \end{document}

enter image description here

The code is set up defining a function which handles your problem. You can change a1,a2,a3 to other coordinates (each component an integer) recompile and get the updated picture.

Sage is not part of the LaTeX distribution. The easiest way to get started is through a free Cocalc account. It's possible to avoid that by downloading Sage to your computer and getting it to work with your LaTeX distribution. This can be easy or, in some cases, difficult. With Cocalc you can be up and running in 5 minutes.

DJP
  • 12,451