# Drawing 2d and 3d vectors using lua or luamplib in LaTeX

Here is the tikz code using which vectors can be drawn.

\documentclass{article}
\usepackage{tikz}
\usepackage{tikz-3dplot}
\usepackage{amsmath}

\begin{document}
\tdplotsetmaincoords{60}{120}
\begin{tikzpicture}[tdplot_main_coords]
% Axes
\draw [->] (0,0,0) -- (3,0,0) node [below left] {$x$};
\draw [->] (0,0,0) -- (0,3,0) node [right] {$y$};
\draw [->] (0,0,0) -- (0,0,3) node [above] {$z$};
% Vectors
\draw [->, thick] (0,0,0) -- (2,2,0);
\draw [->, thick] (0,0,0) -- (2,0,1);

% Labels
\node [below right] at (2,2,0) {$(2,0,1)$};
\node [above  left] at (2,0,1) {$(2,0,1)$};
\end{tikzpicture}
\end{document}


Is it possible to draw vectors using luamplib package? I am seeking for any lua based solution for this problem.

There is no "built in" support for 3D coordinates in "plain" Metapost or in the "metafun" format, but you can program it to do more or less anything.

If you want a simple isometric projection, then you could adapt the approach that I showed last week in this answer. What I did there was to create a macro p(x, y, z) that projects from three dimensions to two dimensions isometrically. But the downsides are (a) you have to remember to type p(x,y,z) all the time instead of just using a triple (x, y, z), and (b) isometric projections don't look very realistic.

If you want something more sophisticated then take a look at these:

But beware that drawing nicely in 3D is very hard, and there are no simple ways to reconcile 3D ideas with some of the core MP concepts. For example:

• what happens to the notion of a closed path? is a cube closed?
• how should you render the thickness of each line? Should lines be thicker if they are nearer the "observer's eye"?
• exactly how can we compute a Bezier spline in 3D?

and so on.

## A worked example

For what it's worth, here is a version of your code using my isometric_projection.mp file:

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
input isometric_projection
set_projection(30, -50);
ipscale := 42;
ahangle := 30;
beginfig(1);
% Axes
drawarrow p(0,0,0) -- p(3,0,0); label.rt("$x$", p(3,0,0));
drawarrow p(0,0,0) -- p(0,3,0); label.top("$y$", p(0,3,0));
drawarrow p(0,0,0) -- p(0,0,3); label.urt("$z$", p(0,0,3));
% Vectors
drawarrow p(0,0,0) -- p(2,2,0) withpen pencircle scaled 1;
drawarrow p(0,0,0) -- p(2,0,1) withpen pencircle scaled 1;
% Labels
label.urt("$(2,2,0)$", p(2,2,0));
label.rt("$(2,0,1)$", p(2,0,1));
endfig;
\end{mplibcode}
\end{document}


If you compile this with lualatex (and you have my isometric_projection.mp available), then you will get this: The relevant portion of the isometric file is this:

newinternal ipca, ipsa, ipcb, ipsb, ipscale;

def set_projection(expr alpha, beta) =
ipca := cosd(alpha); ipsa := sind(alpha);
ipcb := cosd(beta); ipsb := sind(beta);
enddef;

ipscale := 20;

vardef p(expr x, y, z) =
(x * ipcb - z * ipsb, y * ipca + x * ipsa * ipsb + z * ipsa * ipcb) scaled ipscale
enddef;


You can either include this directly in your MP source, or save it in a file called isometric_projection.mp somewhere in your input path and input it as shown above.

• Thanks for your answer. Is it possible to write Lua code that can generate tikz code? Can some command such as \luaplotvector{v,w} be defined which will plot vectors in v and w etc in 3D. ? Jun 24 at 17:45
• that's a separate question... Jun 24 at 20:22