7

I've been studying answers like this one https://tex.stackexchange.com/a/120953/85210 to create "side by side" code examples:

enter image description here

But... I'm not sure about the way to use it when it looks like this:

enter image description here

I mean breaking the line when the width of the tikzpicture is greater than X. Should I make another "class" for examples like this one or it is possible to create some statement and make all in one solution?

My point is to avoid a situation like this:

https://i.stack.imgur.com/Q84qH.png

I don't want to change the original size of the picture. Just break the line.

Let's say that we are using the code created by Thomas F. Sturm in the mentioned answer:

\documentclass{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}

\begin{document}

\begin{sidebyside}[before lower app={\path[] (-2,-0.5) rectangle (2,0.5);}]{4.2cm}
\pgfdeclarehorizontalshading{myshadingA}
  {1cm}{rgb(0cm)=(1,0,0); color(2cm)=(green); color(4cm)=(blue)}
\pgfuseshading{myshadingA}
\end{sidebyside}

\begin{sidebyside}{4.2cm}
\node {root}
  child {node {left}}
  child {node {right}
    child {node {child}}
    child {node {child}}
};
\end{sidebyside}

\begin{sidebyside}[explicitpicture]{4.2cm}
\begin{tikzpicture}[line width=20pt]
  \useasboundingbox (0,-1.5) rectangle (3.5,1.5);
  \draw[red] (0,0) -- (3,0);
  \draw[gray,->] (0,0) -- (3,0);
\end{tikzpicture}
\end{sidebyside}

\end{document}
  • It is not clear to me what is the desired behavior. Are you asking how to squeeze it on a single line, or how to handle it if a single line is not feasible? – Steven B. Segletes Nov 30 '15 at 18:17
  • 1
    @StevenB.Segletes, excuse me for not being clear. It's more about handling it when the image is wider than half of a page. – Pawel Gumiela Nov 30 '15 at 18:21
  • A follow up, if I may. Do you require syntax highlighting, or is a verbatim solution acceptable? – Steven B. Segletes Nov 30 '15 at 18:23
  • 1
    Syntax highlighting is not that important but I wanted to generate these listings automatically. That's how T. Sturm's solution works. – Pawel Gumiela Nov 30 '15 at 18:24
  • By "automatically", I am guessing you mean without having to repeat the code twice (once inside the tikzpicture and again inside a listing)? – Steven B. Segletes Nov 30 '15 at 18:28
  • That's the point. The second advantage is the same layout for all of them. – Pawel Gumiela Nov 30 '15 at 18:29
  • It still isn't clear to me what you want to happen when it is >X. Do you want the code box on below the image? Do you want the image to be scaled down? Do you want the code squeezed? (What if the image takes 95% of the available width?) – cfr Dec 01 '15 at 01:15
  • 1
    @cfr, I would like to keep the original size and don't squeeze the code. My goal is to achieve the same behaviour as shown in the second picture. Now it is impossible. I can change the expliciturpicture as far as I can but it would not break the line: http://i.stack.imgur.com/Q84qH.png – Pawel Gumiela Dec 01 '15 at 07:13

1 Answers1

6

I assume that you want to achieve something like the PGF manual. Perhaps the best way is to have a copy of pgfmanual-en-macros.tex and write:

\documentclass{ltxdoc}
\usepackage[a4paper,left=2.25cm,right=2.25cm,top=2.5cm,bottom=2.5cm,nohead]{geometry}
\usepackage{tikz}
\let\setlength\pgfmathsetlength
\input pgfmanual-en-macros.tex

Now you can write something like the manual:

\begin{document}

\subsubsection{Horizontal and Vertical Shadings}

  Declares a horizontal shading named \meta{shading name} of the specified
  \meta{height} with the specified colors. The width of the bar is
  deduced automatically from the maximum dimension in the specification.

\begin{codeexample}[]
\pgfdeclarehorizontalshading{myshadingA}
  {1cm}{rgb(0cm)=(1,0,0); color(2cm)=(green); color(4cm)=(blue)}
\pgfuseshading{myshadingA}
\end{codeexample}

  The effect of the \meta{color list}, which is a
  comma-separated list of colors, is the following: Normally, when
  this list is empty, once a shading has been declared, it becomes
  ``frozen.'' This means that even if you change a color that was used
  in the declaration of the shading later on, the shading will not
  change. By specifying a \meta{color list} you can specify
  that the shading should be recalculated whenever one of the colors
  listed in the list changes (this includes effects like color
  mixins). Thus, when you specify a \meta{color list},                      % TODOsp: mixins --> mixings? (ff) (or is here really the object meant?)
  whenever the shading is used, \pgfname\ first converts the colors in the
  list to \textsc{rgb} triples using the current values of the
  colors and taking any mixins and blends into account. If the
  resulting \textsc{rgb} triples have not yet been   used, a new
  shading is internally created and used. Note that if the
  option \meta{color list} is used, then no shading is created until
  the first use of |\pgfuseshading|. In particular, the colors
  mentioned in the shading need not be defined when the declaration is
  given.

You can also position labels on curves and, by adding the |sloped|
option, have them rotated such that they match the line's slope. Here
is an example:

\begin{codeexample}[]
\begin{tikzpicture}
  \draw (0,0) .. controls (6,1) and (9,1) ..
    node[near start,sloped,above] {near start}
    node {midway}
    node[very near end,sloped,below] {very near end} (12,0);
\end{tikzpicture}
\end{codeexample}




If you have a copy of pgfmanual-en-main-preamble.tex as well, you can change the line

\input{../../macros/pgfmanual-en-macros}    % TODOsp: same here

to

\input{pgfmanual-en-macros}    % TODOsp: same here

unless you put the file in the right place. And then

\documentclass{ltxdoc}

\input pgfmanual-en-main-preamble.tex
% change the line
%                 \input{../../macros/pgfmanual-en-macros}
% to
%                 \input{pgfmanual-en-macros}

Same content as before, except that pgfmanual-en-main-preamble.tex prepares more commands and settings. (For instance the command environment, layout, and the \setlength.)

\begin{document}

\subsubsection{Horizontal and Vertical Shadings}

\begin{command}{\pgfdeclarehorizontalshading\oarg{color list}\marg{shading
      name}\marg{shading height}\marg{color specification}}
  Declares a horizontal shading named \meta{shading name} of the specified
  \meta{height} with the specified colors. The width of the bar is
  deduced automatically from the maximum dimension in the specification.

\begin{codeexample}[]
\pgfdeclarehorizontalshading{myshadingA}
  {1cm}{rgb(0cm)=(1,0,0); color(2cm)=(green); color(4cm)=(blue)}
\pgfuseshading{myshadingA}
\end{codeexample}

  The effect of the \meta{color list}, which is a
  comma-separated list of colors, is the following: Normally, when
  this list is empty, once a shading has been declared, it becomes
  ``frozen.'' This means that even if you change a color that was used
  in the declaration of the shading later on, the shading will not
  change. By specifying a \meta{color list} you can specify
  that the shading should be recalculated whenever one of the colors
  listed in the list changes (this includes effects like color
  mixins). Thus, when you specify a \meta{color list},                      % TODOsp: mixins --> mixings? (ff) (or is here really the object meant?)
  whenever the shading is used, \pgfname\ first converts the colors in the
  list to \textsc{rgb} triples using the current values of the
  colors and taking any mixins and blends into account. If the
  resulting \textsc{rgb} triples have not yet been   used, a new
  shading is internally created and used. Note that if the
  option \meta{color list} is used, then no shading is created until
  the first use of |\pgfuseshading|. In particular, the colors
  mentioned in the shading need not be defined when the declaration is
  given.

\end{command}

You can also position labels on curves and, by adding the |sloped|
option, have them rotated such that they match the line's slope. Here
is an example:

\begin{codeexample}[]
\begin{tikzpicture}
  \draw (0,0) .. controls (6,1) and (9,1) ..
    node[near start,sloped,above] {near start}
    node {midway}
    node[very near end,sloped,below] {very near end} (12,0);
\end{tikzpicture}
\end{codeexample}


\end{document}

Symbol 1
  • 36,855