lslisting 代码内的 TiKZ 箭头和文本

lslisting 代码内的 TiKZ 箭头和文本

我有一些 lslistings 代码,我想添加一些文本和数学元素,如下图红色所示

在此处输入图片描述

我尝试使用 TiKZ,但出现了很多错误。我附上了一份 MWE,并提前感谢那些能帮助我的人。

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

幻灯片文本:

 \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

答案1

您可以定义一个转义字符,escapechar以便您可以在列表代码中定义 tikz 代码。此外,为了防止 tikz 图形破坏您的列表代码,请激活该overlay选项。

remember picture如果您希望能够引用幻灯片中其他地方的代码,您还可以利用它。

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

逃往 tikz

相关内容