2

I have a problem again, with Tikz and Rectangles. The second rectangle is not being drawn, but the first is. Even if I remove everything except the second fill (including the canvas transform and scope), it won't be drawn either.

\documentclass{beamer}
\usepackage{listings}
\usepackage{tikz}
\usepackage{pgfplots}
\lstset{
   language=C++,
   basicstyle=\ttfamily\scriptsize,
   numbers=left,
   captionpos=b
}
\usetikzlibrary{positioning,arrows,tikzmark,calc}
% This is the file main.tex
\usetheme{Darmstadt}

\begin{document}

\begin{frame}[fragile]
\frametitle{Wiedereintrittsfähiges Interface}
\begin{lstlisting}[mathescape,escapechar=\%]
float flicker_float(bool restart, ActiveConfig<Traits> config,
                    float sample) 
{
   // save to 'state'
   %\tikzmark{statb}%static Impl state;%\tikzmark{state}%
   $\ldots$
}
\end{lstlisting}
\begin{lstlisting}[mathescape,escapechar=\%]
float flicker_float_r(bool restart, ActiveConfig<Traits> config,
                      float sample, %\tikzmark{parb}%Impl *state%\tikzmark{pare}%) 
{
   // save to *state
   $\ldots$
}
\end{lstlisting}
\begin{tikzpicture}[overlay, remember picture]
\begin{scope}[transform canvas={yshift=-2pt}]
  \fill[opacity=.1] (pic cs:statb) rectangle ($(pic cs:state) + (0,10pt)$ -| {pic cs:state});
  \fill[opacity=.1] (pic cs:parb) rectangle ($(pic cs:parb) + (0,10pt)$ -| {pic cs:pare});
  \draw[->] (pic cs:statb) -| ([xshift=-5pt]pic cs:parb) -- +(5pt,0);
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}

If I use filldraw instead of fill, I see a tiny fraction of the left border of the second rectangle. This would suggest a clipping problem, but I'm out of guesses as to what causes it.

2

Seems to be a coordinate problem, not a clipping problem. If you try

\draw (pic cs:parb) -- ($(pic cs:parb) + (0,10pt)$ -| {pic cs:pare});

you'll see that you get a straight vertical line. I.e. the second coordinate ends up directly above the first one. I can't see exactly why it ends up that way, but a couple of simpler approaches could be

\fill[opacity=0.1] (pic cs:parb) rectangle ([yshift=10pt]pic cs:pare);

or

\fill[opacity=0.1] (pic cs:parb) rectangle ($(pic cs:pare) + (0,10pt)$); 

which both work fine.

output of code

\documentclass{beamer}
\usepackage{listings}
\usepackage{pgfplots} % loads tikz
\lstset{
   language=C++,
   basicstyle=\ttfamily\scriptsize,
   numbers=left,
   captionpos=b
}
\usetikzlibrary{positioning,arrows,tikzmark,calc}
% This is the file main.tex
\usetheme{Darmstadt}

\begin{document}

\begin{frame}[fragile]
\frametitle{Wiedereintrittsfähiges Interface}
\begin{lstlisting}[mathescape,escapechar=\%]
float flicker_float(bool restart, ActiveConfig<Traits> config,
                    float sample) 
{
   // save to 'state'
   %\tikzmark{statb}%static Impl state;%\tikzmark{state}%
   $\ldots$
}
\end{lstlisting}
\begin{lstlisting}[mathescape,escapechar=\%]
float flicker_float_r(bool restart, ActiveConfig<Traits> config,
                      float sample, %\tikzmark{parb}%Impl *state%\tikzmark{pare}%) 
{
   // save to *state
   $\ldots$
}
\end{lstlisting}
\begin{tikzpicture}[overlay, remember picture]
\begin{scope}[transform canvas={yshift=-2pt}]
  \fill[opacity=.1] (pic cs:statb) rectangle ($(pic cs:state) + (0,10pt)$ -| {pic cs:state});
  \fill[opacity=0.1] (pic cs:parb) rectangle ([yshift=10pt]pic cs:pare);
% \fill[opacity=0.1] (pic cs:parb) rectangle ($(pic cs:pare) + (0,10pt)$);

% uncomment to see straight line  
%  \draw (pic cs:parb) -- ($(pic cs:parb) + (0,10pt)$ -| {pic cs:pare});

  \draw[->] (pic cs:statb) -| ([xshift=-5pt]pic cs:parb) -- +(5pt,0);
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}

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.