2

I use tikz to draw some computer architecture schematics and I want to highlight some wires.

enter image description here For that, I use a style that creates double lines to create a stabilo-like effect. I include a trick that I recently discovered to fix bounding box computation in double lines.

The problem is that when the draw command is directly issued, the drawing is on the foreground layer and partly hides the gate.

enter image description here

Drawing the gate after the wires is not an option, as the gate pic contains coordinates that are required to draw the wires.

The only solution that I found is to put wires in a background layer scope and this how the first image was generated.

  \begin{scope}[on background layer]
      \draw ... ;
  \end{scope}

In a real computer schematic, there may be 100+ wires and this a solution I would prefer to avoid.
A solution could be to create a macro, but it is not very tikz-ish and it makes difficult to add to the wires labels, branches, coordinates, etc.

So my question is : is there a way to define a style that can be added to lines in order to draw them on the background layer?

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc, intersections,backgrounds}

\makeatletter
%% fix for bb computation of double wires. 
%% from https://tex.stackexchange.com/questions/130456/tikz-double-lines-are-shifted
\tikzset{
  only coordinates are relevant/.is choice,
  only coordinates are relevant/.default=true,
  only coordinates are relevant/true/.code={%
    \tikz@addmode{\pgf@relevantforpicturesizefalse}},
  only coordinates are relevant/false/.code={%
    \tikz@addmode{\pgf@relevantforpicturesizetrue}}
}
\makeatother

\tikzset{
  highlighted/.style={
    line width=0.8pt,
    only coordinates are relevant,
    preaction={
      draw,yellow,-,
      double=yellow,
      double distance=2\pgflinewidth,
      opacity=1
    },
  },  
}


\tikzset{
  mux/.pic={
    \coordinate (-sw) at (0,0) ;
    \coordinate (-nw) at (0,0.6cm) ;
    \coordinate (-se) at (0.25cm,0) ;
    \coordinate (-ne) at (0.25cm,0.6cm) ;
    \draw[line width=0.8pt] (-sw) -- (-nw)
                         -- ($(-se)!0.7!(-ne)$) coordinate (-ne1)
                         -- ($(-se)!0.3!(-ne)$) coordinate (-se1)
                         -- cycle ;
    \coordinate (-I1) at ($(-sw)+(0cm,0.15cm)$) ;
    \coordinate (-I2) at ($(-nw)+(0cm,-0.15cm)$) ;
    \coordinate (-O) at ($(-se)!0.5!(-ne)$) ; 
    \coordinate (-NSel) at ($(-nw)!0.5!(-ne1)$) ;
    \draw[] (-I2) node[anchor=west, inner sep=0] {\tiny 0};
    \draw[] (-I1) node[anchor=west, inner sep=0] {\tiny 1};
  },
}


\begin{document}
% directly issuing draw commands
\begin{tikzpicture}
  \pic at (0,0) (mux1) {mux} ;
  \draw[highlighted] (mux1-I1) -- ++(-1cm,0) ;
  \draw (mux1-I2) -- ++(-1cm,0) ;
  \draw[highlighted] (-1cm,1cm) -| (mux1-NSel) ;
  \draw[highlighted] (mux1-O) -- ++(1cm,0);
\end{tikzpicture}

%% enclosed in backgound layer scope
\begin{tikzpicture}
  \pic (mux1) {mux} ;
  \begin{scope}[on background layer]
      \draw[highlighted] (mux1-I1) -- ++(-1cm,0) ;
  \end{scope}
  \draw[] (mux1-I2) -- ++(-1cm,0) ;
  \begin{scope}[on background layer]
    \draw[highlighted] (-1cm,1cm) -| (mux1-NSel) ;
  \end{scope}
  \begin{scope}[on background layer]
    \draw[highlighted] (mux1-O) -- ++(1cm,0);
  \end{scope}
\end{tikzpicture}

\end{document}
3

https://tex.stackexchange.com/a/419123/2388 shows an implementation of an on layer style:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc, intersections,backgrounds}

\tikzset{%
  on layer/.code={
    \pgfonlayer{#1}\begingroup
    \aftergroup\endpgfonlayer
    \aftergroup\endgroup
  }}

\makeatletter
%% fix for bb computation of double wires.
%% from https://tex.stackexchange.com/questions/130456/tikz-double-lines-are-shifted
\tikzset{
  only coordinates are relevant/.is choice,
  only coordinates are relevant/.default=true,
  only coordinates are relevant/true/.code={%
    \tikz@addmode{\pgf@relevantforpicturesizefalse}},
  only coordinates are relevant/false/.code={%
    \tikz@addmode{\pgf@relevantforpicturesizetrue}}
}
\makeatother

\tikzset{
  highlighted/.style={
    line width=0.8pt,
    only coordinates are relevant,
    preaction={
      draw,yellow,-,
      double=yellow,
      double distance=2\pgflinewidth,
      opacity=1,
      on layer=background
    },
  },
}


\tikzset{
  mux/.pic={
    \coordinate (-sw) at (0,0) ;
    \coordinate (-nw) at (0,0.6cm) ;
    \coordinate (-se) at (0.25cm,0) ;
    \coordinate (-ne) at (0.25cm,0.6cm) ;
    \draw[line width=0.8pt] (-sw) -- (-nw)
                         -- ($(-se)!0.7!(-ne)$) coordinate (-ne1)
                         -- ($(-se)!0.3!(-ne)$) coordinate (-se1)
                         -- cycle ;
    \coordinate (-I1) at ($(-sw)+(0cm,0.15cm)$) ;
    \coordinate (-I2) at ($(-nw)+(0cm,-0.15cm)$) ;
    \coordinate (-O) at ($(-se)!0.5!(-ne)$) ;
    \coordinate (-NSel) at ($(-nw)!0.5!(-ne1)$) ;
    \draw[] (-I2) node[anchor=west, inner sep=0] {\tiny 0};
    \draw[] (-I1) node[anchor=west, inner sep=0] {\tiny 1};
  },
}


\begin{document}
% directly issuing draw commands
\begin{tikzpicture}
  \pic at (0,0) (mux1) {mux} ;
  \draw[highlighted] (mux1-I1) -- ++(-1cm,0) ;
  \draw (mux1-I2) -- ++(-1cm,0) ;
  \draw[highlighted] (-1cm,1cm) -| (mux1-NSel) ;
  \draw[highlighted] (mux1-O) -- ++(1cm,0);
\end{tikzpicture}

\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.