3

I have some lslistings code and I want to add some text and math elements as is shown in red color in the image below

enter image description here

I tried using TiKZ but I got many errors. I'm attaching a MWE and thanks in advance to whom can help me.

\documentclass[xcolor={dvipsnames,svgnames,x11names}]{beamer}
\usepackage{amsmath,amssymb,amsfonts,amsthm}
\usepackage{color,graphicx}
\usepackage{pgf,tikz,pgfplots,pgfkeys}
\usetikzlibrary{%
    arrows,
    arrows.meta,
    cd,
    matrix,
    decorations.pathreplacing,
    positioning,
    calc,
    fit,
    shapes,
    shapes.symbols,
    shapes.multipart,%rectangle split
    shapes.arrows,
    backgrounds,
    snakes,
    shadows.blur,
    trees,%for probability trees
    shadows,% drop shadows
    fadings
}
\pgfplotsset{compat=1.15}
\usepackage{mathrsfs}
\usepackage{multirow}
\usepackage{changepage}
\usepackage{makecell}
% Choose how your presentation looks.
%   
%
\mode<presentation>
{
  \usetheme{Madrid}      % or try Darmstadt, Madrid, Warsaw, ...
  \usecolortheme{beaver} % or try albatross, beaver, crane, ...
  \usefonttheme{serif}  % or try serif, structurebold, ...
  \setbeamertemplate{navigation symbols}{}
  \setbeamertemplate{caption}[numbered]
} 

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}

\usepackage{listings}
\lstset
{
    language=[LaTeX]TeX,
    breaklines=true,
    basicstyle=\tt\scriptsize,
    %commentstyle=\color{green}
    keywordstyle=\color{blue},
    %stringstyle=\color{black}
    identifierstyle=\color{magenta},
}

%=========================================================================================
\newcommand\mznfont{\fontfamily{pcr}\selectfont}

\lstdefinelanguage{Mzn}
{
    morekeywords={
        %
        array, par, var, opt, constraint, solve, satisfy, minimize,
        maximize, output, include, let, in, set, of, if, then, else, endif,
        ann, annotation, bool, enum, float, int, string, where, function,
        predicate, true, false, not, assert, trace,
        % ???:
        any, list, op, record, test, tuple, type
        %
    },
    %
    keywords=[2]{
        %
        forall, sliding_sum, symmetry_breaking_constraint,
        implied_constraint, redundant_constraint, all_different,
        alldifferent, alldifferent_except_0, alldiff, element, circuit,
        subcircuit, card, bool2int, inter, count, regular, table, xor,
        exists, xorall, iffall, clause, intersect, diff, symdiff, subset,
        superset, concat, join, length, int_lt_reif, int_lin_eq_reif,
        bool_imply, array_bool_or, all_different_int, int_ne, at_least,
        at_most, exactly, count_eq, count_leq, count_geq, count_gt, nvalue,
        bin_packing, bin_packing_capa, bin_packing_load, diffn,
        global_cardinality, global_cardinality_closed,
        global_cardinality_low_up, global_cardinality_low_up_closed,
        inverse, cumulative, disjunctive, decreasing, increasing, sort,
        arg_sort, value_precede, value_precede_chain, lex_less, lex_lesseq,
        lex_greater, lex_greatereq, lex2, int_lin_eq, bool_lin_eq, knapsack,
        partition_set, member, reverse,
        % from ???:
        abort, abs, acosh, array_intersect, array_union, array1d, array2d,
        array3d, array4d, array5d, array6d, asin, atan, bool2int, card,
        ceil, concat, cos, cosh, dom, dom_array, dom_size, fix, exp, floor,
        index_set, index_set_1of2, index_set_2of2, index_set_1of3,
        index_set_2of3, index_set_3of3, index_set_6of6, int2float, is_fixed,
        join, lb, lb_array, length, ln, log, log2, log10, min, max, pow,
        product, round, set2array, show, show_int, show_float, sin, sinh,
        sqrt, sum, tan, tanh, trace, ub, ub_array, xor, in, subset,
        superset, union, diff, symdiff, intersect, div, mod,
        % annotations:
        is_defined_var, output_var, var_is_introduced, defines_var,
        promise_total, bounds, domain, bool_search, int_search, seq_search,
        set_search, input_order, first_fail, anti_first_fail, smallest,
        largest, occurrence, most_constrained, max_regret, indomain_min,
        indomain_max, indomain_middle, indomain_median, indomain,
        indomain_random, indomain_split, indomain_reverse_split,
        indomain_interval, outdomain_max, outdomain_median, outdomain_min,
        outdomain_random, complete
        % 
    },
    sensitive=true,
    basicstyle=\mznfont,
    commentstyle=\color[rgb]{0.9,0.1,0.1},
    keywordstyle=\color[rgb]{0,0.5,0},
    keywordstyle=[2]\color{blue},
    stringstyle=\color{orange},
    tabsize=2,
    frame=none,
    % identifierstyle = \it,
    numbers=left,
    stepnumber=1,
    numberstyle=\tiny,
    numbersep=5pt,
    xleftmargin=0pt, % numbers will be in the margins!
    columns=fixed, % same width for all characters
    % columns=flexible,
    % columns=fullflexible,
    morecomment=[l]{\%},
    morestring=[b]",
    % morestring=[d]',
    showstringspaces=false,
    mathescape=true,
    breaklines=true,
    % prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\space\red{\swarrow}}},%\hookrightarrow
    %postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookrightarrow\space}},%\hookrightarrow
    breakatwhitespace=true,
    breakindent=10pt, % was: 20pt
    moredelim=**[is][\color{Melon}]{@}{@},
    escapeinside={{<@}{@>}}
}
%% Write "\begin{frame}[fragile]" for a slide using either of the
%% following two listing environments, which have unnumbered
%% respectively numbered lines:
\lstnewenvironment{mzn}[1][]{\lstset{language=Mzn,#1}}{}
\lstnewenvironment{mznno}[1][]{\lstset{language=Mzn,numbers=none,xleftmargin=0pt,#1}}{}
%% Inline a code snippet, without respectively with the comprehension bar (|):
\newcommand{\mzninline}[1]{\lstinline[{language=Mzn}]|#1|}
\newcommand{\mzninlinebar}[1]{\lstinline[{language=Mzn}]!#1!}
%==========================================================================================

\title[2019}
\author{2019}
\institute{2019}
\date{2019}

\AtBeginSection[]
{
  \begin{frame}<beamer>
    \frametitle{Outline}
    \tableofcontents[currentsection,currentsubsection]
  \end{frame}
}

\begin{document}

\begin{frame}
  \titlepage
\end{frame}

% Uncomment these lines for an automatically generated outline.
\begin{frame}{Outline}
  \tableofcontents
\end{frame}

\begin{frame}{Slide}
\input{slide}
\end{frame}

\end{document}

Slide.text:

 \begingroup
 \tiny
 \begin{lstlisting}[frame=single,basicstyle=\tiny, language=Mzn]
 lorem(lorem in $ \alpha $..lorem where
 (lorem != lorem /\
 (lorem[x, lorem, $ \alpha $] == lorem[x, lorem, $ \alpha $]) /\ 
 (lorem[x, lorem, $ \alpha $] == $ \alpha $  \/
 lorem[p, lorem, $ \alpha $] == lorem[x, lorem, $ \alpha $])))
 (
 \end{lstlisting}
 \endgroup
  • There's an extension to the tikzmark library which add lots of tikzmarks in a listings environment. It originated in the answer tex.stackexchange.com/a/86457/86 . See the tikzmark documentation for more details. – Loop Space Jun 18 at 20:35
4

You could define an escape character with escapechar so that you can define your tikz code in your listing code. In addition, to prevent the tikz figure from disrupting your listing code, activate the overlay option.

You could furthermore make use of remember picture if you want to be able to reference the code from elsewhere in the slide.

\documentclass{beamer}

\usepackage{listings}
\usepackage{tikz}
\tikzset{font=\small}


\begin{document}

\begin{frame}[fragile]

\begin{lstlisting}[frame=single,basicstyle=\tiny,escapechar = ~~] % I chose ~~ as escape character
   lorem(lorem in $ \alpha $..lorem where
   (lorem != lorem /\
   (lorem[x, lorem, $ \alpha $] == lorem[x, lorem, $ \alpha $]) /\ ~~\tikz[overlay]{\draw[thick,red,->] (0,0.5ex) -- ++(0.5,0) node[right] {Lorem}}~~
   (lorem[x, lorem, $ \alpha $] == $ \alpha $  \/ 
   lorem[p, lorem ~~\tikz[remember picture]{\coordinate (P1) at (-2em,-1ex)}~~, $ \alpha $] 
   == lorem[x, lorem, $ \alpha~~\tikz[overlay]{\draw[thick,red,->] (-1em,-1ex) |- ++(1,-1) node[right] {Lorem}}~~ $])))
   (
\end{lstlisting}
\vspace{2cm}
\begin{itemize}
    \item<1> Look here! \tikz[overlay,remember picture]{\draw[thick,red,->] (-1.5em,1.5ex) -- (P1)}
\end{itemize}

\end{frame}

\end{document}

escape to tikz

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.