5

I am preparing a second "edition" of my minimal guide to TikZ and would like to have more professional looking presentation for the example. Thanks to Thomas F. Strun this is for the most part easy - I use the code he proposed in Code examples, like in the TikZ/PGF manual. However, I have a few examples where the figure is too wide for the sidebyside presentation and where I would like the figure to be on top of the code. I have for the most part succeeded in doing this by adapting the code, but I do not seem to be able to get the right background behind the picture. This is what I get (with a simple side by side example first to show the colors): two examples

and this is the code I use

 \documentclass[10pt]{article} 

\usepackage[most]{tcolorbox}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
%
\lstdefinestyle{example}{style=tcblatex,
  classoffset=0,
  texcsstyle=*\color{blue},%
  deletetexcs={begin,end},
  moretexcs={,%
    pgfdeclarehorizontalshading,pgfuseshading,node,
    useasboundingbox,draw}%
  classoffset=1,
  keywordstyle=\color{blue},%
  morekeywords={tikzpicture,shade,fill,draw,path,node,child,line,width,rectangle},
  classoffset=0}
%
\tcbset{%
  fillbackground/.style={before lower pre={%
  \tikzset{every picture/.style={execute at end picture={\begin{pgfonlayer}{background}
    \fill[yellow!25!white]
    ([xshift=-1mm,yshift=-1mm]current bounding box.south west) rectangle
    ([xshift=1mm,yshift=1mm]current bounding box.north east);
    \end{pgfonlayer}}}}}},
  explicitpicture/.style={before lower=\begin{center},after lower=\end{center},fillbackground}}
%
\newtcblisting{sidebyside}[2][]{%
  enhanced,frame hidden,
  top=0pt,bottom=0pt,left=0pt,right=0pt,arc=0pt,boxrule=0pt,
  colback=blue!25!white,
  listing style=example,
  sidebyside,text and listing,text outside listing,sidebyside gap=2mm,
  lefthand width=#2,tikz lower,fillbackground,
  #1}
%
\newtcblisting{pictureontop}[2][]{%
 enhanced,frame hidden,
top=0pt,bottom=0pt,left=0pt,right=0pt,arc=0pt,boxrule=0pt,
colback=blue!25!white,
 listing style=example,
text and listing, %text outside listing, sidebyside gap=2mm,
%  lefthand width=#2, tikz lower,
lower separated=false,
fillbackground,
  #1}
\begin{document}
\begin{sidebyside}{4.2cm}
\begin{tikzpicture}
\draw (0,0) -- (2,1);
\end{tikzpicture}
\end{sidebyside}
%
\begin{pictureontop}[explicitpicture]{4.2cm}
\begin{tikzpicture}[xscale=1.3]
\draw [thick]  (0,0) -- (9,0);
\draw (0,-.2) -- (0, .2);
\draw (3,-.2) -- (3, .2);
\draw (6,-.2) -- (6, .2);
\draw (9,-.2) -- (9, .2);
\node[align=left, below] at (1.5,-.5)%
    {This happens\\in period 1\\and is aligned\\ left};
\node[align=center, below] at (4.5,-.5)%
    {This happens\\in period 2\\and is centered};
\node[align=right, below] at (7.5,-.5)%
    {This happens\\in period 2\\and is right\\aligned};
\end{tikzpicture}
\end{pictureontop}
\end{document}

I would appreciate help putting yellow behind my figure. (And it would be wonderful if I also knew how to put a optional page break between the figure and the code, but this is much less important.)

5

Following code uses text above listing instead of text and listing in pictureontop definition.

And as can be seen, code box is moved to next page as soon as there is no enough space.

\documentclass[10pt]{article} 

\usepackage{lipsum}

\usepackage[most]{tcolorbox}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
%
\lstdefinestyle{example}{style=tcblatex,
  classoffset=0,
  texcsstyle=*\color{blue},%
  deletetexcs={begin,end},
  moretexcs={,%
    pgfdeclarehorizontalshading,pgfuseshading,node,
    useasboundingbox,draw}%
  classoffset=1,
  keywordstyle=\color{blue},%
  morekeywords={tikzpicture,shade,fill,draw,path,node,child,line,width,rectangle},
  classoffset=0}
%
\tcbset{%
  fillbackground/.style={before lower pre={%
  \tikzset{every picture/.style={execute at end picture={\begin{pgfonlayer}{background}
    \fill[yellow!25!white]
    ([xshift=-1mm,yshift=-1mm]current bounding box.south west) rectangle
    ([xshift=1mm,yshift=1mm]current bounding box.north east);
    \end{pgfonlayer}}}}}},
  explicitpicture/.style={before lower=\begin{center},after lower=\end{center},fillbackground}}
%
\newtcblisting{sidebyside}[2][]{%
  enhanced,frame hidden,
  top=0pt,bottom=0pt,left=0pt,right=0pt,arc=0pt,boxrule=0pt,
  colback=blue!25!white,
  listing style=example,
  sidebyside,text and listing,text outside listing,sidebyside gap=2mm,
  lefthand width=#2,tikz lower,fillbackground,
  #1}
%
\newtcblisting{pictureontop}[2][]{%
 enhanced,frame hidden,
top=0pt,bottom=0pt,left=0pt,right=0pt,arc=0pt,boxrule=0pt,
colback=blue!25!white,
 listing style=example,
text above listing, 
lower separated=false,
fillbackground,
  #1}
\begin{document}
\begin{sidebyside}{4.2cm}
\begin{tikzpicture}
\draw (0,0) -- (2,1);
\end{tikzpicture}
\end{sidebyside}

\lipsum[1-3]

%
\begin{pictureontop}[explicitpicture]{4.2cm}
\begin{tikzpicture}[xscale=1.3]
\draw [thick]  (0,0) -- (9,0);
\draw (0,-.2) -- (0, .2);
\draw (3,-.2) -- (3, .2);
\draw (6,-.2) -- (6, .2);
\draw (9,-.2) -- (9, .2);
\node[align=left, below] at (1.5,-.5)%
    {This happens\\in period 1\\and is aligned\\ left};
\node[align=center, below] at (4.5,-.5)%
    {This happens\\in period 2\\and is centered};
\node[align=right, below] at (7.5,-.5)%
    {This happens\\in period 2\\and is right\\aligned};
\end{tikzpicture}
\end{pictureontop}
\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.