# Drawing diagrams related to laser optics

I came across this collection of svg images for drawing diagrams related to laser optics. It is called "ComponentLibrary" and it is developed by Alexander Franzen.

I used it in the past by importing the various images into an svg editor. Then, by connecting lines between them and inserting text nodes, I got very nice results: I was thinking of creating similar images in tikz, and create such figures and diagrams with tikz only.

But I would like to get some advice from the more experienced tikz users here about the dimensions and positions of such small images.

For example, I created this image of a sensor: Its tikz code is

% file sensor.tikz
\begin{tikzpicture}
\draw[line width=0.25] (0,0) rectangle (1,1);
\draw[very thin] (1.3,0.45) rectangle (1.6,0.55);
\draw[very thin, rotate around={25:(0.75,0.5)}] (1.6,0.55) rectangle (1.3,0.45);
\draw[very thin, rotate around={-25:(0.75,0.5)}] (1.6,0.55) rectangle (1.3,0.45);
\end{tikzpicture}


I chose to create a rectangle with sides of length 1, with its corners at (0,0) and (1,1).

Now I want to add a wavy arrow to indicate some EM radiation hitting it: So I plotted a wavy arrow from (4,0.5) to (2,0.5), which I think is not ideal. Better is to instruct tikz to plot an arrow from "a bit to the right of the sensor, until the sensor".

%\usetikzlibrary{decorations.pathmorphing}
\begin{tikzpicture}
\input{sensor.tikz}
\path[->, draw=red,decorate,decoration={snake,pre length=3pt,post length=7pt}] (4,0.5) -- node[above] {Radiation} (2,0.5);
\end{tikzpicture}


I would like to ask for your thoughts about writing a collection of .tikz files, mainly about what positions should I set to each optics component, and about pre defining nodes in each image.

Because once you have more than 3 or 4 images on your canvas, you have to be able to control their position and relation more systematically.

• The best (but hardest) solution is to create shapes (see section 106.5, pages 1143-1149). Other options include pics and scopes with named local bouding boxes. The easiest would be to precompile the shapes using standalone and add the images using nodes with \includegraphics. Oct 23, 2022 at 15:08
• That's a good proposal. E.g. you could start with place-holder shapes, which don't show a drawing yet, but which you can already use to try different "laser drawings", becaue they provide width and height etc. When you fill them one by one with content, i.e. drawings, your drawings should show more and more refinement. // This would be a top-down approach. Oct 23, 2022 at 15:22
• @JohnKormylo I don't think there is a closed, concise answer to this question, and I think that your suggestion is the one pgf developers were thinking of. So I suggest that you would write it as an answer.
– tush
Oct 23, 2022 at 15:29
• @tush - You want me to do the hard one? (Sigh!) Oct 23, 2022 at 15:39

This version uses a savebox, however due to the way [remember picture] works, it can only be used once. You also need to run it twice.

\documentclass{standalone}% so I don't have to crop it
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}

\newsavebox{\sensor}
\savebox{\sensor}{\begin{tikzpicture}[remember picture]
\draw[line width=0.25] (0,0) rectangle (1,1);
\draw[very thin] (1.3,0.45) rectangle (1.6,0.55)
++(0.1,-0.05) coordinate[right] (sensor 2);
\draw[very thin, rotate around={25:(0.75,0.5)}] (1.3,0.45) rectangle (1.6,0.55)
++(0.1,-0.05) coordinate[right] (sensor 1);
\draw[very thin, rotate around={-25:(0.75,0.5)}] (1.3,0.45) rectangle (1.6,0.55)
++(0.1,-0.05) coordinate[right] (sensor 3);
\end{tikzpicture}}

\begin{document}

\begin{tikzpicture}[remember picture]
\node[inner sep=0pt] at (0,0) {\usebox\sensor};
\path[<-, draw=red,decorate,decoration={snake,pre length=3pt,post length=7pt}]
(sensor 1) ++(25:0.1) -- node[above, sloped] {Radiation} ++(25:2);
\path[<-, draw=red,decorate,decoration={snake,pre length=3pt,post length=7pt}]
(sensor 2) ++(0:0.1) -- node[above, sloped] {Radiation} ++(0:2);
\path[<-, draw=red,decorate,decoration={snake,pre length=3pt,post length=7pt}]
(sensor 3) ++(-25:0.1) -- node[above, sloped] {Radiation} ++(-25:2);
\end{tikzpicture}
\end{document}


This version creates the shape sensor. It differs from a standand TikZ shape in that it does not expand to fit the text inside. Also, I located the center anchor at the point where the sensors radiate outwand, making it assymetrical.

It automatically draws the shape, as opposed to using [draw]. This also makes it difficult to change colors. See \pgfusepath for options.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}

\makeatletter
\pgfdeclareshape{sensor}{% locate center at pivot point, not symmetrical
\savedanchor{\southwest}{\pgfpoint{-0.75cm}{-0.5cm}}%
\savedanchor{\northeast}{\pgfpoint{0.85cm}{0.5cm}}%
\anchor{center}{\pgfpointorigin}%
\anchorborder{\pgfpointborderrectangle{\southwest}{\northeast}}%
\anchor{south west}{\southwest}%
\anchor{south}{\southwest \pgf@x=0pt}%
\anchor{west}{\southwest \pgf@y=0pt}%
\anchor{south east}{\southwest \pgf@x=0.85cm}%
\anchor{north east}{\northeast}%
\anchor{north}{\northeast \pgf@x=0pt}%
\anchor{east}{\northeast \pgf@y=0pt}%
\anchor{north west}{\northeast \pgf@x=-0.75cm}%
{\pgfpoint{-.5\wd\pgfnodeparttextbox}{-.5\ht\pgfnodeparttextbox}}}%
\anchor{sensor 1}{\pgfpointpolar{25}{0.95cm}}%
\anchor{sensor 2}{\pgfpoint{0.95cm}{0pt}}%
\anchor{sensor 3}{\pgfpointpolar{-25}{0.95cm}}%
\foregroundpath{\pgfsetlinewidth{0.25pt}%
\pgfpathrectanglecorners{\southwest}{\pgfpoint{0.25cm}{0.5cm}}%
\pgfpathrectanglecorners{\pgfpoint{0.55cm}{-0.05cm}}{\pgfpoint{0.85cm}{0.05cm}}%
\pgfusepath{draw}%
\begin{pgfscope}%
\pgftransformrotate{25}%
\pgfpathrectanglecorners{\pgfpoint{0.55cm}{-0.05cm}}{\pgfpoint{0.85cm}{0.05cm}}%
\pgfusepath{draw}%
\end{pgfscope}%
\begin{pgfscope}%
\pgftransformrotate{-25}%
\pgfpathrectanglecorners{\pgfpoint{0.55cm}{-0.05cm}}{\pgfpoint{0.85cm}{0.05cm}}%
\pgfusepath{draw}%
\end{pgfscope}%
}}%
\makeatother

\begin{document}

\begin{tikzpicture}
\node[sensor] (A) at (0,0) {name};
\path[<-, draw=red,decorate,decoration={snake,pre length=3pt,post length=7pt}]
(A.sensor 1) ++(25:0.1) -- node[above, sloped] {Radiation} ++(25:2);
\path[<-, draw=red,decorate,decoration={snake,pre length=3pt,post length=7pt}]
(A.sensor 2) ++(0:0.1) -- node[above, sloped] {Radiation} ++(0:2);
\path[<-, draw=red,decorate,decoration={snake,pre length=3pt,post length=7pt}]
(A.sensor 3) ++(-25:0.1) -- node[above, sloped] {Radiation} ++(-25:2);
\end{tikzpicture}
\end{document}

• Thats very impressive.
– tush
Oct 23, 2022 at 17:31
{"ft_lang_label":"__label__en","ft_lang_prob":0.9035617,"math_prob":0.9754357,"size":1850,"snap":"2023-40-2023-50","text_gpt3_token_len":539,"char_repetition_ratio":0.097508125,"word_repetition_ratio":0.0,"special_character_ratio":0.2837838,"punctuation_ratio":0.18439716,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9871024,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,1,null,1,null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-02T03:25:27Z\",\"WARC-Record-ID\":\"<urn:uuid:2d641670-29bb-467f-a0c9-4367c00fed2a>\",\"Content-Length\":\"152043\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:037f3019-6582-4542-96a2-47f98fe70d26>\",\"WARC-Concurrent-To\":\"<urn:uuid:ceb2943a-9ad1-4fca-ad02-8682498d352d>\",\"WARC-IP-Address\":\"172.64.144.30\",\"WARC-Target-URI\":\"https://tex.stackexchange.com/questions/662746/drawing-diagrams-related-to-laser-optics\",\"WARC-Payload-Digest\":\"sha1:ZC5KSAA4P3HXI333AFK2UYFFXHIEEYIU\",\"WARC-Block-Digest\":\"sha1:FGFIB6T4JPKIY7ZUS4KCEXRD5MTWBU3W\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100309.57_warc_CC-MAIN-20231202010506-20231202040506-00804.warc.gz\"}"}