# Drawing diffraction gratings

I'm new to tikz and I'm having trouble drawing this. How can I do it?

• Welcome to TeX.SX! On this site, a question should typically revolve around an abstract issue (e.g. "How do I get a double horizontal line in a table?") rather than a concrete application (e.g. "How do I make this table?"). Questions that look like "Please do this complicated thing for me" tend to get closed because they are either "off topic", "too broad", or "unclear". Please try to make your question clear and simple by giving a minimal working example (MWE): you'll stand a greater chance of getting help. May 28, 2021 at 17:56
• Unrelated, but I could have an anxiety stroke seeing those two planes that are not parallel. Now, this sketch is not too long to write but doing it for you is too easy. If you want to be able to draw further things, you may want to learn a bit. Grab the manual, try some thinkgs and come back with specific questions about how to fix that. That's the best way to get answers. May 28, 2021 at 18:01
• @SebGlav Maybe, the planes are not parallel for some reason?
– hola
May 28, 2021 at 18:13
• Unrelated question about optics or whatever topic this figure is about (based on fellow SebGlav's comment): Does it matter that the planes are not parallel?
– hola
May 28, 2021 at 18:16
• @SebGlav I'm glad to see you are still alive to see the beautiful answers. 😉😁
– hola
May 29, 2021 at 9:50

You could do something like the following isometric view, with tikz and the 3d library. I made a \newcommand macro for the "Red" and "Pantalla", because there are the same figure with different dimensions (and colors), and the rest are lines circles and arcs, each one in a plane defined with the 3d library (canvas is... options).

\documentclass[border=2mm]{standalone}
\usepackage    {tikz}
\usetikzlibrary{3d}    % For "canvas is..." options
\usetikzlibrary{babel} % There are conflicts between tikz and some babel packages

% isometric axes
\pgfmathsetmacro\yx{1/sqrt(2)}
\pgfmathsetmacro\yy{1/sqrt(6)}
\pgfmathsetmacro\xy{sqrt(2/3)}

\newcommand{\rectangle}[6]% position (z), x,y,z dimensions, color, label
{%
\draw[canvas is xy plane at z=#1,fill=#5,fill opacity=0.8] (-#2,-#3)   rectangle (#2,#3);
\draw[canvas is xz plane at y=#3,fill=#5]                  (-#2,#1-#4) rectangle (#2,#1);
\draw[canvas is yz plane at x=#2,fill=#5]                  (-#3,#1-#4) rectangle (#3,#1);
\node at (#2,-#3,#1) [above left,rotate=30] {#6};
}
\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,%
x={(0 cm,\xy cm)},y={(-\yx cm,-\yy cm)},z={(\yx cm,-\yy cm)}]
% Dimensions
\def\ph{2}   % Puntero, height
\def\rz{5}   % Red, position (z)
\def\ra{1.5} % Red, semi-dimension x
\def\rb{1.7} % Red, semi-dimension y
\def\rc{0.1} % Red, dimension z
\def\pz{10}  % Pantalla, postion (z)
\def\pa{\ra} % Pantalla, semi-dimension x
\def\pb{3}   % Pantalla, semi-dimension y
\def\pc{0.1} % Pantalla, dimension z
% Puntero
\draw[top color=gray] (-45:\pr)     --++ (0,0,\ph)  node[sloped,midway,above] {Puntero}
arc (-45:135:\pr) --++ (0,0,-\ph) arc (135:-45:\pr);
\draw[canvas is xy plane at z=\ph,fill=white] (0,0) circle (\pr);
% Single ray
\draw[red,thick] (0,0,\ph) -- (0,0,\rz);
% Red
\rectangle{\rz}{\ra}{\rb}{\rc}{brown!30}{Red}
% Triple rays
\foreach\i in {-1,0,1}
{%
\draw[red,thick] (0,0,\rz) -- (0,0.5*\pb*\i,\pz);
}
% Pantalla
\rectangle{\pz}{\pa}{\pb}{\pc}{white}{Pantalla}
\begin{scope}[canvas is xy plane at z=\pz]
\foreach\i in {-1,0,1}
{%
\draw[dashed] (0,0.5*\pb*\i) --++   (-1.5*\pa,0);
\fill[red]    (0,0.5*\pb*\i) circle (2pt);
\ifnum \i < 1
\draw[<->] (-1.5*\pa,0.5*\pb*\i) --++ (0,0.5*\pb) node [midway,below] {$z$};
\fi
}
\end{scope}
\draw[dashed] (-\ra,\rb,\rz-0.5*\rc)   -- (-\ra,\pb+1,\rz-0.5*\rc);
\draw[dashed] (-\pa,\pb,\pz-0.5*\rc)   -- (-\pa,\pb+1,\pz-0.5*\pc);
\draw[<->]    (-\ra,\pb+1,\rz-0.5*\rc) -- (-\pa,\pb+1,\pz-0.5*\pc) node[midway,below] {$D$};
\node at (0,0,\pz) [above] {$O$};
\end{tikzpicture}
\end{document}


P.S.: I'm supposing, as SebGlav commented, that the planes "Red" and "Pantalla" must be parallel.

• Very nice code and drawing! Far better than the original! May 28, 2021 at 20:00
• @SebGlav, thanks!!! By the way, I completely agree with you. I don't like the not so parallel planes. And neither the distance D. :-P May 28, 2021 at 20:09

Just for fun created with Mathcha....It is very closer with your image. This is a unique piece. :-)

\documentclass[a4paper,12pt]{article}
\usepackage{tikz}

\begin{document}

\tikzset {_xxxnodc6j/.code = {\pgfsetadditionalshadetransform{ \pgftransformshift{\pgfpoint{0 bp } { 0 bp }  }  \pgftransformrotate{0 }  \pgftransformscale{2 }  }}}
rgb(37.5bp)=(1,1,1);
rgb(45.24972098214286bp)=(0.73,0.71,0.71);
rgb(56.339285714285715bp)=(0.95,0.95,0.95);
rgb(62.5bp)=(0.96,0.96,0.96);
rgb(100bp)=(0.96,0.96,0.96)}
\tikzset{every picture/.style={line width=0.75pt}} %set default line width to 0.75pt

\begin{tikzpicture}[x=0.75pt,y=0.75pt,yscale=-1,xscale=1]
%uncomment if require: \path (0,439); %set diagram left start at 0, and has height of 439

%Straight Lines [id:da14737216884669246]
\draw  [dash pattern={on 0.84pt off 2.51pt}]  (411.5,291) -- (411.5,405) ;
%Straight Lines [id:da7656742806470096]
\draw  [dash pattern={on 0.84pt off 2.51pt}]  (540.5,157.5) -- (540.5,271.5) ;
%Straight Lines [id:da2702625665256513]
\draw  [dash pattern={on 0.84pt off 2.51pt}]  (478,219) -- (478,338.5) ;
%Straight Lines [id:da6484742549495406]
\draw [color={rgb, 255:red, 208; green, 2; blue, 27 }  ,draw opacity=1 ][line width=1.5]    (101,199.71) -- (200,199.71) ;
%Shape: Cube [id:dp8551957300719828]
\draw  [fill={rgb, 255:red, 224; green, 230; blue, 182 }  ,fill opacity=1 ] (173,174.33) -- (235.14,112.2) -- (239,112.2) -- (239,213.86) -- (176.86,276) -- (173,276) -- cycle ; \draw   (239,112.2) -- (176.86,174.33) -- (173,174.33) ; \draw   (176.86,174.33) -- (176.86,276) ;
%Shape: Can [id:dp07108459318332927]
\path  [shading=_n765rn7ok,_xxxnodc6j] (104,207.43) -- (28,207.43) .. controls (26.34,207.43) and (25,204.42) .. (25,200.71) .. controls (25,197.01) and (26.34,194) .. (28,194) -- (104,194) .. controls (105.66,194) and (107,197.01) .. (107,200.71) .. controls (107,204.42) and (105.66,207.43) .. (104,207.43) .. controls (102.34,207.43) and (101,204.42) .. (101,200.71) .. controls (101,197.01) and (102.34,194) .. (104,194) ; % for fading
\draw  [color={rgb, 255:red, 0; green, 0; blue, 0 }  ,draw opacity=1 ] (104,207.43) -- (28,207.43) .. controls (26.34,207.43) and (25,204.42) .. (25,200.71) .. controls (25,197.01) and (26.34,194) .. (28,194) -- (104,194) .. controls (105.66,194) and (107,197.01) .. (107,200.71) .. controls (107,204.42) and (105.66,207.43) .. (104,207.43) .. controls (102.34,207.43) and (101,204.42) .. (101,200.71) .. controls (101,197.01) and (102.34,194) .. (104,194) ; % for border

%Shape: Ellipse [id:dp43485916585372353]
\draw  [color={rgb, 255:red, 208; green, 2; blue, 27 }  ,draw opacity=1 ] (206.07,190.93) .. controls (207.96,189.1) and (209.5,190.31) .. (209.5,193.62) .. controls (209.5,196.93) and (207.96,201.1) .. (206.07,202.93) .. controls (204.18,204.76) and (202.64,203.55) .. (202.64,200.24) .. controls (202.64,196.93) and (204.18,192.76) .. (206.07,190.93) -- cycle ;
%Straight Lines [id:da7472104356246656]
\draw [color={rgb, 255:red, 208; green, 2; blue, 27 }  ,draw opacity=1 ][line width=1.5]    (206.07,196.93) -- (544,152) ;
%Straight Lines [id:da6874394911762671]
\draw [color={rgb, 255:red, 208; green, 2; blue, 27 }  ,draw opacity=1 ][line width=1.5]    (206.07,196.93) -- (471,224) ;
%Straight Lines [id:da10191189772940112]
\draw [color={rgb, 255:red, 208; green, 2; blue, 27 }  ,draw opacity=1 ][line width=1.5]    (407,290) -- (206.07,196.93) ;
%Shape: Cube [id:dp9219515675909264]
\draw  [fill={rgb, 255:red, 255; green, 255; blue, 255 }  ,fill opacity=0.71 ] (393,181.02) -- (551.02,23) -- (556,23) -- (556,233.98) -- (397.98,392) -- (393,392) -- cycle ; \draw   (556,23) -- (397.98,181.02) -- (393,181.02) ; \draw   (397.98,181.02) -- (397.98,392) ;
%Shape: Circle [id:dp12691254054231815]
\draw  [fill={rgb, 255:red, 208; green, 2; blue, 27 }  ,fill opacity=1 ] (536,153) .. controls (536,149.96) and (538.46,147.5) .. (541.5,147.5) .. controls (544.54,147.5) and (547,149.96) .. (547,153) .. controls (547,156.04) and (544.54,158.5) .. (541.5,158.5) .. controls (538.46,158.5) and (536,156.04) .. (536,153) -- cycle ;
%Straight Lines [id:da02978669418743607]
\draw    (413.62,402.88) -- (475.88,340.62) ;
\draw [shift={(478,338.5)}, rotate = 495] [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.08]  [draw opacity=0] (8.93,-4.29) -- (0,0) -- (8.93,4.29) -- cycle    ;
\draw [shift={(411.5,405)}, rotate = 315] [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.08]  [draw opacity=0] (8.93,-4.29) -- (0,0) -- (8.93,4.29) -- cycle    ;
%Straight Lines [id:da2557715976074917]
\draw    (482.12,333.38) -- (540.88,274.62) ;
\draw [shift={(543,272.5)}, rotate = 495] [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.08]  [draw opacity=0] (8.93,-4.29) -- (0,0) -- (8.93,4.29) -- cycle    ;
\draw [shift={(480,335.5)}, rotate = 315] [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.08]  [draw opacity=0] (8.93,-4.29) -- (0,0) -- (8.93,4.29) -- cycle    ;
%Straight Lines [id:da7574961163436977]
\draw  [dash pattern={on 0.84pt off 2.51pt}]  (395.5,315) -- (395.5,429) ;
%Straight Lines [id:da0712410751453958]
\draw  [dash pattern={on 0.84pt off 2.51pt}]  (173.98,265) -- (173.98,429) ;
%Straight Lines [id:da4806322026107146]
\draw    (176.98,429) -- (392.5,429) ;
\draw [shift={(395.5,429)}, rotate = 180] [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.08]  [draw opacity=0] (10.72,-5.15) -- (0,0) -- (10.72,5.15) -- (7.12,0) -- cycle    ;
\draw [shift={(173.98,429)}, rotate = 0] [fill={rgb, 255:red, 0; green, 0; blue, 0 }  ][line width=0.08]  [draw opacity=0] (10.72,-5.15) -- (0,0) -- (10.72,5.15) -- (7.12,0) -- cycle    ;
%Shape: Circle [id:dp4658659851312721]
\draw  [fill={rgb, 255:red, 208; green, 2; blue, 27 }  ,fill opacity=1 ] (472,224) .. controls (472,220.96) and (474.46,218.5) .. (477.5,218.5) .. controls (480.54,218.5) and (483,220.96) .. (483,224) .. controls (483,227.04) and (480.54,229.5) .. (477.5,229.5) .. controls (474.46,229.5) and (472,227.04) .. (472,224) -- cycle ;
%Shape: Circle [id:dp5607627449126491]
\draw  [fill={rgb, 255:red, 208; green, 2; blue, 27 }  ,fill opacity=1 ] (406,291) .. controls (406,287.96) and (408.46,285.5) .. (411.5,285.5) .. controls (414.54,285.5) and (417,287.96) .. (417,291) .. controls (417,294.04) and (414.54,296.5) .. (411.5,296.5) .. controls (408.46,296.5) and (406,294.04) .. (406,291) -- cycle ;

% Text Node
\draw (446.75,375.15) node [anchor=north west][inner sep=0.75pt]    {$Z$};
% Text Node
\draw (513.5,307.4) node [anchor=north west][inner sep=0.75pt]    {$Z$};
% Text Node
\draw (484.5,198.4) node [anchor=north west][inner sep=0.75pt]    {$O$};
% Text Node
\draw (169,112) node [anchor=north west][inner sep=0.75pt]   [align=left] {\textbf{{\large Red}}};
% Text Node
\draw (420,47) node [anchor=north west][inner sep=0.75pt]  [font=\large] [align=left] {\textbf{Pantalla}};
% Text Node
\draw (34,164) node [anchor=north west][inner sep=0.75pt]   [align=left] {\textbf{Puntero}};
% Text Node
\draw (284.75,405.15) node [anchor=north west][inner sep=0.75pt]    {$D$};

\end{tikzpicture}

\end{document}