# How can I draw a box in an arbitrary surface using TikZ?

I would like to draw a box around an arbitrary surface in a very simple sketch. I have found this thread about drawing arbitrary surfaces with pgfplot. Ideally, I would like to remove the gridlines and so on (but I can work this out).

The problem is that I don't know how to draw the box. The box should be an arbitrary volume which crosses the surface. The box should be an extrusion of a small piece of the surface. Please see the sketch below. Any ideas? Thank you very much in advance! EDIT: I have got this so far. The problem is that when I plot the curves using \addplot3 it plots them as closed curves, so I get a bunch of extra lines joining what should be the ends of my curves.

\begin{tikzpicture}
\pgfmathsetmacro\z{2*cos(1)}
\begin{axis}[
hide axis,
scale=2,
view={120}{40},
xmin=-4,xmax=4,
ymin=-4,ymax=4,
zmin=-2,zmax=10,
]
\addplot3 [ surf, colormap/bone, domain=-4:4, domain y=-4:4,
samples=30, samples y=30,
variable=\u, variable y=\v,
point meta=u*v ]
( {u}, {v}, {cos(u) + cos(v)} );

\addplot3[black, thick, dashed, variable=\t, domain=-1:1] ( 1, {t}, {cos(1) + cos(t) - 2});
\addplot3[black, thick, variable=\t, domain=-1:1] ( -1, {t}, {cos(1) + cos(t) - 2});
\addplot3[black, thick, variable=\t, domain=-1:1] ( {t}, 1, {cos(1) + cos(t) - 2});
\addplot3[black, thick, variable=\t, domain=-1:1] ( {t}, -1, {cos(1) + cos(t) - 2});

\addplot3 [ surf, domain=-1:1, domain y=-1:1,
samples=30, samples y=30,
variable=\u, variable y=\v,
point meta=u*v ]
( {u}, {v}, {cos(u) + cos(v)} );

\addplot3[black, thick, variable=\t, domain=-1:1] ( 1, t, {cos(1) + cos(t) + 2});
\addplot3[black, thick, variable=\t, domain=-1:1] ( -1, {t}, {cos(1) + cos(t) + 2});
\addplot3[black, thick, variable=\t, domain=-1:1] ( {t}, 1, {cos(1) + cos(t) + 2});
\addplot3[black, thick, variable=\t, domain=-1:1] ( {t}, -1, {cos(1) + cos(t) + 2});

\addplot3[black, thick, variable=\t, domain=-1:1] ( 1, {t}, {cos(1) + cos(t)});
\addplot3[black, thick, variable=\t, domain=-1:1] ( -1, {t}, {cos(1) + cos(t)});
\addplot3[black, thick, variable=\t, domain=-1:1] ( {t}, 1, {cos(1) + cos(t)});
\addplot3[black, thick, variable=\t, domain=-1:1] ( {t}, -1, {cos(1) + cos(t)});

\draw[dashed, thick] (1,1,\z-2) -- (1,1,\z+2);
\end{axis}
\end{tikzpicture}

• You can see as starting point an example of tikz-3dplot tex.stackexchange.com/questions/158585/…. – Sebastiano Oct 24 '17 at 9:59
• Welcome to TeX.SX! Questions of the form "Please draw this for me", which show no effort on the part of OP, often don't get answered. You will get more help if you post some code showing what you have tried and give a minimal working example. For example, since you say that you can (probably) do this, a good start would be to post code removing the grid lines from the post you refer to. This will give people code to start from and make it much more likely some one will hep you. – Andrew Oct 24 '17 at 10:00

Difficult - although not impossible - to generalise:

\documentclass[tikz,margin=5]{standalone}
\begin{document}
\begin{tikzpicture}[x=(330:1.8cm),y=(30:1.8cm),z=(90:1cm),
declare function={z(\t,\u)=-0.125*sin(\t*180)-0.25*sin(\u*180);}]
\foreach \k/\drw/\fll/\z in
{1/black/blue!20/0, 0.4/none/white/0,0.4/black/blue!20/0.5}
\path[draw=\drw,fill=\fll]
plot [domain=-\k:\k] ( \x,  \k, {z( \x, \k)+\z}) --
plot [domain=\k:-\k] ( \k,  \x, {z( \k, \x)+\z}) --
plot [domain=\k:-\k] ( \x, -\k, {z( \x,-\k)+\z}) --
plot [domain=-\k:\k] (-\k,  \x, {z(-\k, \x)+\z}) -- cycle;
\draw [dotted]
( .4, .4, {z( .4, .4)+.5}) -- ( .4, .4, {z( .4, .4)})
(-.4,-.4, {z(-.4,-.4)+.5}) -- (-.4,-.4, {z(-.4,-.4)})
( .4,-.4, {z( .4,-.4)+.5}) -- ( .4,-.4, {z( .4,-.4)});
\end{tikzpicture}
\end{document} Here's an effort in Metapost that might inspire a different approach in TikZ. prologues := 3;
outputtemplate := "%j%c.eps";

beginfig(1);

path surface, box_floor, box_top;

color sky; sky = 7/8[blue, white];

z0 = z4 = origin;
z1 = 120 right rotated -10;
z2 = z1 shifted (50, 80);
z3 = z2 shifted -z1;

r = 28;
surface = for i=0 upto 3: z[i] { (z[i+1]-z[i]) rotated r } .. { (z[i+1]-z[i]) rotated r } z[i+1] & endfor cycle;

fill surface withcolor sky;
draw surface;

path a, b, c, d;
a = subpath(0,1) of surface shifted .28(z3-z0);
b = subpath(1,2) of surface shifted .28(z4-z1);
c = subpath(2,3) of surface shifted .28(z1-z2);
d = subpath(3,4) of surface shifted .28(z2-z3);

box_floor =
(a cutbefore d cutafter b) ..
(b cutbefore a cutafter c) ..
(c cutbefore b cutafter d) ..
(d cutbefore c cutafter a) .. cycle;

box_top = box_floor shifted 13 up;

unfill box_floor;
draw box_floor;

for i = 0 step 2 until 4:
draw point i of box_floor .. point i of box_top dashed withdots scaled 1/4;
endfor

fill box_top withcolor sky;
draw box_top;

endfig;
end.


This is plain Metapost: compile with mpost to produce an .eps file, or adapt for gmp package or luamplib package in LaTeX.