Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

If possible I would like to know how to this on latex/tikz to add this like (environment and graph) on my ebook.

Thanks a lot!!!

How to do this on latex or tickz ?

Code im trying to implement:

\documentclass[11pt,fleqn]{book} 

\usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,headsep=10pt,a4paper]{geometry} % Page margins

\usepackage{xcolor} % Required for specifying colors by name
\definecolor{ocre}{RGB}{243,102,25} % Define the orange color used for highlighting throughout the book

% Font Settings
\usepackage{avant} % Use the Avantgarde font for headings
%\usepackage{times} % Use the Times font for headings
\usepackage{mathptmx} % Use the Adobe Times Roman as the default text font together with math symbols from the Sym­bol, Chancery and Com­puter Modern fonts

\usepackage{microtype} % Slightly tweak font spacing for aesthetics
\usepackage[utf8]{inputenc} % Required for including letters with accents
\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs



% Index
\usepackage{calc} % For simpler calculation - used for spacing the index letter headings correctly
\usepackage{makeidx} % Required to make an index
\makeindex % Tells LaTeX to create the files required for indexing

\usepackage{titlesec} % Allows customization of titles

\usepackage{graphicx} % Required for including pictures
\graphicspath{{Pictures/}} % Specifies the directory where pictures are stored

\usepackage{lipsum} % Inserts dummy text

\usepackage{tikz} % Required for drawing custom shapes

\usepackage[brazil]{babel} % English language/hyphenation

\usepackage{enumitem} % Customize lists
\setlist{nolistsep} % Reduce spacing between bullet points and numbered lists

\usepackage{booktabs} % Required for nicer horizontal rules in tables

\usepackage{eso-pic} % Required for specifying an image background in the title page

%----------------------------------------------------------------------------------------
% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12}  % for normal

% Custom colors - http://get-software.net/info/colour/chroma/chroma.pdf
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\definecolor{Keywords}{rgb}{0,0,1}
\definecolor{functions}{rgb}{0.75,0,0.5}
\definecolor{boolean}{rgb}{1,0.4,0}

%\usepackage{listings}
\usepackage[procnames]{listings}

% Python style for highlighting
\newcommand\pythonstyle{\lstset{
language=Python,
basicstyle=\ttm,
otherkeywords={self},             % Add keywords here
keywordstyle=\ttb\color{deepblue},
%% keywords
emph={MyClass,__init__},          % Custom highlighting
emphstyle=\ttb\color{deepred},    % Custom highlighting style
%% keywords
stringstyle=\color{deepgreen},
frame=tb,                         % Any extra options here
%% keywords
emph={[2]or,and,not,def,for,in,if},                 % Custom highlighting
emphstyle=[2]\ttb\color{boolean},       % Custom highlighting style
%% keywords
emph={[3]input,print,range,int,float,str},                  % Custom highlighting
emphstyle=[3]\ttb\color{functions},     % Custom highlighting style
%%
procnamekeys={def,class},
procnamestyle=\color{blue}\ttb,
%%
commentstyle=\color{red}\ttm,
numbers=left,
numberstyle=\footnotesize,
numbersep=1em,
showstringspaces=false,            %
%%
rulecolor=\color{ocre}, % Cor da linha de borda
framexleftmargin=1mm, framextopmargin=1mm,
}}


% Python environment
\lstnewenvironment{python}[1][]
{
\pythonstyle
\lstset{#1}
}
{}

% Python for external files
\newcommand\pythonexternal[2][]{{
\pythonstyle
\lstinputlisting[#1]{#2}}}

% Python for inline
\newcommand\pythoninline[1]{{\pythonstyle\lstinline!#1!}}

%----------------------------------------------------------------------------------------
%----------------------------------------------------------------------------------------


\begin{document}

\begin{python}[backgroundcolor=\color{black!5}]
Python sintax: test of arguments

print('bla bla',234)

\end{python}

\end{document}

Result:

enter image description here

share|improve this question
    
What do you mean by the "graph"? The lines pointing at the function's arguments? Or the graphical layout of the box, with frame, colours, etc.? –  Christoph B. Feb 6 at 16:52
4  
Welcome to TeX.SX. Questions about how to draw specific graphics that just post an image of the desired result are really not reasonable questions to ask on the site. Please post a minimal compilable document showing that you've tried to produce the image and then people will be happy to help you with any specific problems you may have. See minimal working example (MWE) for what needs to go into such a document. –  Paul Gessler Feb 6 at 16:53
    
Actually, boths, the frame with border, and the lines pointing to the function arguments. –  André Feb 6 at 16:57

1 Answer 1

up vote 23 down vote accepted

One option would be to use the tcolorbox package and its listings library:

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{fixltx2e}
\tcbuselibrary{listings}

\definecolor{color1}{RGB}{244,243,224}
\definecolor{color2}{RGB}{31,174,174}

\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\definecolor{Keywords}{rgb}{0,0,1}
\definecolor{functions}{rgb}{0.75,0,0.5}
\definecolor{boolean}{rgb}{1,0.4,0}

\tcbset{listing engine=listings}

\newtcblisting{mybox}[1][]{%
  colback=yellow!5,
  colframe=color2,
  colbacktitle=color1,
  coltitle=black,
  listing only,
  title={\parbox[t]{2cm}{\textit{Syntax:}}\parbox[t]{\dimexpr\linewidth-2cm\relax}{#1}},
  fonttitle=\ttfamily,
  listing options={
    language=Python,
    basicstyle=\small\ttfamily,
    keywordstyle=\ttfamily\color{deepblue},
    stringstyle=\color{deepgreen},
    commentstyle=\ttfamily\color{red},
    showstringspaces=false,
    columns=fullflexible
  }
}

\begin{document}

\begin{mybox}[print()\\print(value\textsubscript{1},value\textsubscript{2},\ldots,value\textsubscript{n})]
print("The answer is", 6 + 7, "!" )
\end{mybox}

\end{document}

enter image description here

Depending on the actual needs, another option would be to use a "regular" tcolorbox; the tikzmark library for tikz was used to place some marks at designated elements and then those marks were used to draw the connecting lines:

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{fixltx2e}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\definecolor{color1}{RGB}{244,243,224}
\definecolor{color2}{RGB}{31,174,174}

\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\definecolor{Keywords}{rgb}{0,0,1}
\definecolor{functions}{rgb}{0.75,0,0.5}
\definecolor{boolean}{rgb}{1,0.4,0}

\lstset{
    language=Python,
    basicstyle=\small\ttfamily,
    keywordstyle=\ttfamily\color{deepblue},
    stringstyle=\color{deepgreen},
    commentstyle=\ttfamily\color{red},
    showstringspaces=false,
    columns=fullflexible,
    escapeinside=||,
}

\newcommand\Text[2][0.5\linewidth]{%
  \parbox[t]{#1}{#2}%
}

\newtcolorbox{mybox}[1][]{%
  colback=yellow!5,
  colframe=color2,
  colbacktitle=color1,
  coltitle=black,
  title={\parbox[t]{2cm}{\textit{Syntax:}}\parbox[t]{\dimexpr\linewidth-2cm\relax}{#1}},
  fonttitle=\ttfamily
}

\begin{document}

\begin{mybox}[print()\\print(value\textsubscript{1},value\textsubscript{2},\ldots,value\textsubscript{n})]

\hfill\Text[.6\linewidth]{All arguments are optional. If no arguments are given, a blank\tikzmark{a} line is printed.}

\vspace{1cm}

\begin{lstlisting}
               print|\tikzmark{b}|("The |\tikzmark{d}|answer is", 6 |\tikzmark{e}|+ 7, "|\tikzmark{f}|!")|\tikzmark{c}|
\end{lstlisting}

\vspace{1cm}

\Text{The values to\tikzmark{g} be printed, one after the other, separated by a blank space}

\end{mybox}

\begin{tikzpicture}[remember picture,overlay,red!80!black]
\draw 
  ([yshift=8pt,xshift=4pt]pic cs:b) --+(0,10pt) 
  -| coordinate[near start] (aux) ([yshift=8pt,xshift=-4pt]pic cs:c);
\draw (aux) -- ([yshift=-8pt,xshift=-4pt]pic cs:a);
\foreach \i in {d,e,f}
  \draw ([yshift=10pt]pic cs:g) -- ([yshift=-5pt]pic cs:\i);
\end{tikzpicture}

\end{document}

enter image description here

This second approach can also be implemented using mdframed:

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{fixltx2e}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\definecolor{color1}{RGB}{244,243,224}
\definecolor{color2}{RGB}{31,174,174}

\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\definecolor{Keywords}{rgb}{0,0,1}
\definecolor{functions}{rgb}{0.75,0,0.5}
\definecolor{boolean}{rgb}{1,0.4,0}

\lstset{
    language=Python,
    basicstyle=\small\ttfamily,
    keywordstyle=\ttfamily\color{deepblue},
    stringstyle=\color{deepgreen},
    commentstyle=\ttfamily\color{red},
    showstringspaces=false,
    columns=fullflexible,
    escapeinside=||,
}

\newcommand\Text[2][0.5\linewidth]{%
  \parbox[t]{#1}{#2}%
}

\newcommand\Title[1]{%
\parbox[t]{2cm}{\textit{Syntax:}}\parbox[t]{\dimexpr\linewidth-2cm\relax}{#1}%
}

\newmdenv[
  roundcorner=6pt,
  backgroundcolor=yellow!5,
  linecolor=color2,
  frametitlefont=\normalfont\ttfamily,
  frametitlerule=true,
  frametitlebackgroundcolor=color1,
]{mybox}

\begin{document}

\begin{mybox}[frametitle=\Title{print()\\print(value\textsubscript{1},value\textsubscript{2},\ldots,value\textsubscript{n})}]

\hfill\Text[.6\linewidth]{All arguments are optional. If no arguments are given, a blank\tikzmark{a} line is printed.}

\vspace{1cm}

\begin{lstlisting}
               print|\tikzmark{b}|("The |\tikzmark{d}|answer is", 6 |\tikzmark{e}|+ 7, "|\tikzmark{f}|!")|\tikzmark{c}|
\end{lstlisting}

\vspace{1cm}

\Text{The values to\tikzmark{g} be printed, one after the other, separated by a blank space}

\end{mybox}

\begin{tikzpicture}[remember picture,overlay,red!80!black]
\draw 
  ([yshift=8pt,xshift=4pt]pic cs:b) --+(0,10pt) 
  -| coordinate[near start] (aux) ([yshift=8pt,xshift=-4pt]pic cs:c);
\draw (aux) -- ([yshift=-8pt,xshift=-4pt]pic cs:a);
\foreach \i in {d,e,f}
  \draw ([yshift=10pt]pic cs:g) -- ([yshift=-5pt]pic cs:\i);
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
Excelent!! Do you know how can I add those lines to explain or to give some notes to each argument's function ? Cheers! –  André Feb 6 at 17:18
1  
@CodeMocker I used just to have easy access to \textsubscript. –  Gonzalo Medina Feb 6 at 17:44
1  
@CodeMocker a remanent of a previous version of the code in which I had used math mode. I'll fix it in some minutes. –  Gonzalo Medina Feb 6 at 18:20
1  
@CodeMocker fixed. Thanks. –  Gonzalo Medina Feb 6 at 18:30
1  
@André I've updated my answer with the second approach now implemented using mdframed. –  Gonzalo Medina Feb 6 at 18:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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