2

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:

enter image description here

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:

enter image description here

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:

enter image description here

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.

4
  • 2
    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.
    – MS-SPO
    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

1 Answer 1

2

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}%
  \anchor{text}{\pgfpointadd{\pgfpoint{-0.25cm}{0pt}}%
    {\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}
1
  • Thats very impressive.
    – tush
    Oct 23, 2022 at 17:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .