3

This question already has an answer here:

I used the following code for displaying the tikzpicture code alongside it.

Is there an easy way to automate displaying the code (centered alongside the picture) without modifying it.

\documentclass{article}
\usepackage{verbatim}
\usepackage{tikz}
\begin{document}
\begin{tabular}{cc}
\begin{tikzpicture}
\draw[thick, blue] (1,0) -- (0,0) -- (0,1) -- cycle;
\end{tikzpicture}
 & 
\textbackslash begin\{tikzpicture\}\\
 & \textbackslash draw[thick, blue] (1,0) -- (0,0) -- (0,1) -- cycle;\\
 & \textbackslash end\{tikzpicture\}
 \\
\end{tabular}
\end{document}

enter image description here

marked as duplicate by CarLaTeX, siracusa, Raaja, CampanIgnis, Sebastiano May 30 at 6:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

You could just use the style from the pgfmanual. Details can be found here. Note that you may have to modify the path in

\input{/usr/local/texlive/2019/texmf-dist/doc/generic/pgf/macros/pgfmanual-en-macros.tex}

on your machine.

\documentclass{ltxdoc} % the pgf manual styles are based on ltxdoc
\usepackage{tikz}
% necessary for the manual styles:
\usepackage{calc}
\input{/usr/local/texlive/2019/texmf-dist/doc/generic/pgf/macros/pgfmanual-en-macros.tex}
\RequirePackage{pgfmanual}
\begin{document}
\begin{codeexample}[]
\begin{tikzpicture}
\draw[thick, blue] (1,0) -- (0,0) -- (0,1) -- cycle;
\end{tikzpicture}
\end{codeexample}
\end{document}

enter image description here

Or you could, similarly to CarLaTeX, use a tcolorbox. The author of the package provides us with a style that looks like the pgfmanual.

\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}[explicitpicture]{4.2cm}
\begin{tikzpicture}
\draw[thick, blue] (1,0) -- (0,0) -- (0,1) -- cycle;
\end{tikzpicture}

\end{sidebyside}

\end{document}
6

With tcolorbox.

You can customize as you like.

\documentclass{article}
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{tcolorbox}
\tcbuselibrary{listings}
\newtcblisting{mybox}{%
    text side listing,
    lefthand width=1.5cm,
    }

\begin{document}
\begin{mybox}
\begin{tikzpicture}
\draw[thick, blue] (1,0) -- (0,0) -- (0,1) -- cycle;
\end{tikzpicture}
\end{mybox}
\end{document}

enter image description here

  • My compliments for your short code. – Sebastiano May 30 at 7:33

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