1

I am trying to draw a diagram that looks approximately like this: enter image description here The red boxes are the items I would like to add to the diagram. They represent spectra lines that can be generated using the pgf-spectra package (https://ctan.org/pkg/pgf-spectra?lang=en). When I try to use a scope to move the box like in my not working example below, the spectra box is not moved. I was wondering if it were possible to move the spectra boxes at all, either in the document or by changing code in the package .sty files

My MNWE:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgf-spectra}
\usetikzlibrary{decorations.pathmorphing}
\tikzset{snake it/.style={decorate, decoration=snake}}
\begin{document}
    \begin{tikzpicture}
    \draw[-latex] (0,0) ++ (60:1.5) arc (60:120:1.5);
    \draw[-latex] (0,0) ++ (-120:1.5) arc (-120:-60:1.5);
    \draw[snake it,blue,-latex,thick] (0,1) -- (-4,1);
    \draw[snake it,red,-latex,thick] (0,-1) -- (-4,-1);
    \draw[snake it,-latex,thick] (-1,0) -- (-4,0);
    \draw[fill=yellow!50] (0,0) circle (1);
% -- this part below is what I want to move but it does not behave correctly
    \begin{scope}{xshift=-4cm,yshift=1cm}
        \pgfspectra[width=5cm]
    \end{scope}
\end{tikzpicture}
\end{document}

Here is the output it renders:

enter image description here

  • The package is actually nesting tikzpictures which may explain odd behavior. – TeXnician Jan 3 at 7:39
  • So one fix would be to use the pgfspectra as the main one and do all the rest of my drawing with respect to the zero point that pgfspectra sets? If that's the case, in my original question I wanted multiple pgfspectra bars at different locations in the one tikzpicture, is that possible? – sab hoque Jan 3 at 7:44
  • The package concept is flawed. I've added a workaround but it's not perfect. The best solution would be to contact the package author and show him your use case that his package does not support. Maybe he will provide an update. – TeXnician Jan 3 at 7:50
  • Can't you just put the spectra in \saveboxes, which is the usual way to avoid nesting tikzpictures? – user121799 Jan 3 at 10:39
2

WARNING: This is a workaround for a simple use case. It does not provide any of the complex options the package will offer. If you wanted these, the package would need to be fixed.

With a bit of reverse engineering you can get something like the following. It copies some internal parts from pgf-spectra to avoid nesting tikzpictures which are preventing your scope from shifting. You may still set width and height keys.

pgf-spectra

\documentclass{article}
\usepackage{tikz}
\usepackage{pgf-spectra}
\usetikzlibrary{decorations.pathmorphing}
\tikzset{snake it/.style={decorate, decoration=snake}}
\begin{document}
    \begin{tikzpicture}
    \draw[-latex] (0,0) ++ (60:1.5) arc (60:120:1.5);
    \draw[-latex] (0,0) ++ (-120:1.5) arc (-120:-60:1.5);
    \draw[snake it,blue,-latex,thick] (0,1) -- (-4,1);
    \draw[snake it,red,-latex,thick] (0,-1) -- (-4,-1);
    \draw[snake it,-latex,thick] (-1,0) -- (-4,0);
    \draw[fill=yellow!50] (0,0) circle (1);
        \makeatletter
    \begin{scope}[shift={(-10cm,.5cm)}]
      % adapted from pgf-spectra.sty
      \pgfkeys{/wl/.cd,width=5cm,height=1cm}
      \foreach \x in {380,...,780}%
        {%
        \pgfmathparse{\wlwidth/(abs(\wlend-\wlbegin))}\edef\xscale{\pgfmathresult}
        \edef\wl@linewidth{\xscale pt}
        \wlcolor{\x}%
        \pgfmathparse{(\x-380)*\xscale}\edef\wl@currentx{\pgfmathresult pt}%
        \draw[wltemp,line width=\wl@linewidth] (\wl@currentx,0) -- ++(0,\wlheight);%
        }%
    \end{scope}
    \makeatother
\end{tikzpicture}
\end{document}
  • Hmm. I will try to copy some code from the internal code and see if I can get the other settings (like absorption lines and start and end) Thanks for the help. – sab hoque Jan 3 at 7:53
  • I can't seem to find the code that specifies the line width and specific emission lines in the internal code, could you tell me which line numbers they are or make an edit? – sab hoque Jan 3 at 8:08
  • 1
    @sabhoque Probably somewhere near all those auxiliary functions at the end but as I said this is more of a workaround because the package concept is bad. If you want to get all of its features you should consider returning to the package's syntax, position them with means like parboxes and then use overlays to get the connections. – TeXnician Jan 3 at 8:16
  • Did exactly that see below – sab hoque Jan 3 at 9:02
  • 1
    @sabhoque Above (+1) ;) – TeXnician Jan 3 at 9:06
2

I have made a workaround using TeXnician's suggestions:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgf-spectra}
\usetikzlibrary{decorations.pathmorphing}
\tikzset{snake it/.style={decorate, decoration=snake}}
\begin{document}
    \setlength{\unitlength}{1cm}
    \thicklines
    \begin{picture}(0,0)
    \put(-5,2.5) {\parbox{4cm}{\pgfspectra[lines={390,490,540,690},absorption,height=0.5cm,width=5cm]}}
    \put(-5,2) {\parbox{4cm}{\pgfspectra[lines={395,495,545,695},absorption,height=0.5cm,width=5cm]}}
    \put(-5,1.5) {\parbox{4cm}{\pgfspectra[lines={400,500,550,700},absorption,height=0.5cm,width=5cm]}}
    \put(-5,1) {\parbox{4cm}{\pgfspectra[lines={405,505,555,705},absorption,height=0.5cm,width=5cm]}}
    \put(-5,0.5) {\parbox{4cm}{\pgfspectra[lines={410,510,560,710},absorption,height=0.5cm,width=5cm]}}
    \put(-5,-0.5) {\parbox{14cm}{\pgfspectra[lines={400,500,550,700},absorption,height=0.5cm,width=5cm,line width=4pt]}}
    \end{picture}
    \begin{tikzpicture}
    \draw[-latex] (0,0) ++ (60:1.5) arc (60:120:1.5);
    \draw[-latex] (0,0) ++ (-120:1.5) arc (-120:-60:1.5);
    \draw[snake it,blue,-latex,thick] (0,1) -- (-4,1);
    \draw[snake it,red,-latex,thick] (0,-1) -- (-4,-1);
    \draw[snake it,-latex,thick] (-1,0) -- (-4,0);
    \draw[fill=yellow!50] (0,0) circle (1);
    \end{tikzpicture}
    \tikz[overlay,remember picture]{\node at (-2.8,-0.5) {Widening of absorption lines};}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.