突出显示 XML 代码投影仪

突出显示 XML 代码投影仪

为了突出显示 XML 代码,我参考了XML 语法突出显示,所以我写:

\documentclass[10pt]{beamer}
\usepackage{listings}

\usepackage{color}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}

\lstset{
basicstyle=\ttfamily,
columns=fullflexible,
showstringspaces=false,
commentstyle=\color{gray}\upshape
}

\lstdefinelanguage{XML}
{
  morestring=[b]",
  morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>},
  stringstyle=\color{black},
 identifierstyle=\color{darkblue},
 keywordstyle=\color{cyan},
 morekeywords={xmlns,version,type}% list your attributes here
 }

\begin{document}

\begin{frame}[fragile]{Structure}

\lstset{language=XML}
\begin{lstlisting}
<?xml version="1.0"  encoding="iso-8859-1"?>
<country>
    <name> France </name>
    <city> Paris </city>
    <region>
        <name> Nord-Pas De Calais </name>
        <population> 3996 </population>
        <city> Lille </city>
   </region>
   <region>
       <name> Valle du Rhone </name>
       <city> Lyon </city>
       <city> Valence </city>
       </region>
</country>
\end{lstlisting}
\end{frame}

\end{document}

现在我想把一些标签放在证据中,给它们上色或者放在彩色矩形中,我找了这两个帖子标记伪代码块并在其附近插入注释或者Beamer - 源代码突出显示/注释提示但我不明白它是如何工作的。

答案1

为了回答这个问题,我从标记伪代码块并在其附近插入注释。我认为甚至可以采用丹尼尔的方法,如下所示Beamer - 源代码突出显示/注释提示(也许将来我也会用这种方式来完成答案)。

这种方法的关键点是在环境中使用转义字符lstlisting。这可以通过以下方式实现:

\lstset{
escapechar=* % <=  to escape to LaTeX
}

完整代码如下:

\documentclass[10pt]{beamer}
\usepackage{lmodern}
\usepackage{listings}

\usepackage{tikz}
\usetikzlibrary{calc}

% to change colors
\newcommand{\fillcol}{orange!20}
\newcommand{\bordercol}{black}

\makeatletter
% code from Andrew Stacey (with small adjustment to the border color)
% https://tex.stackexchange.com/questions/51582/background-coloring-with-overlay-specification-in-algorithm2e-beamer-package
\newcounter{jumping}
\resetcounteronoverlays{jumping}

\def\jump@setbb#1#2#3{%
  \@ifundefined{jump@#1@maxbb}{%
    \expandafter\gdef\csname jump@#1@maxbb\endcsname{#3}%
  }{%
    \csname jump@#1@maxbb\endcsname
    \pgf@xa=\pgf@x
    \pgf@ya=\pgf@y
    #3
    \pgfmathsetlength\pgf@x{max(\pgf@x,\pgf@xa)}%
    \pgfmathsetlength\pgf@y{max(\pgf@y,\pgf@ya)}%
    \expandafter\xdef\csname jump@#1@maxbb\endcsname{\noexpand\pgfpoint{\the\pgf@x}{\the\pgf@y}}%
  }
  \@ifundefined{jump@#1@minbb}{%
    \expandafter\gdef\csname jump@#1@minbb\endcsname{#2}%
  }{%
    \csname jump@#1@minbb\endcsname
    \pgf@xa=\pgf@x
    \pgf@ya=\pgf@y
    #2
    \pgfmathsetlength\pgf@x{min(\pgf@x,\pgf@xa)}%
    \pgfmathsetlength\pgf@y{min(\pgf@y,\pgf@ya)}%
    \expandafter\xdef\csname jump@#1@minbb\endcsname{\noexpand\pgfpoint{\the\pgf@x}{\the\pgf@y}}%
  }
}

\tikzset{%
  remember picture with id/.style={%
    remember picture,
    overlay,
    draw=\bordercol,
    save picture id=#1,
  },
  save picture id/.code={%
    \edef\pgf@temp{#1}%
    \immediate\write\pgfutil@auxout{%
      \noexpand\savepointas{\pgf@temp}{\pgfpictureid}}%
  },
  if picture id/.code args={#1#2#3}{%
    \@ifundefined{save@pt@#1}{%
      \pgfkeysalso{#3}%
    }{
      \pgfkeysalso{#2}%
    }
  },
  onslide/.code args={<#1>#2}{%
    \only<#1>{\pgfkeysalso{#2}}%
  },
  alt/.code args={<#1>#2#3}{%
    \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}%
  },
  stop jumping/.style={
    execute at end picture={%
      \stepcounter{jumping}%
      \immediate\write\pgfutil@auxout{%
        \noexpand\jump@setbb{\the\value{jumping}}{\noexpand\pgfpoint{\the\pgf@picminx}{\the\pgf@picminy}}{\noexpand\pgfpoint{\the\pgf@picmaxx}{\the\pgf@picmaxy}}
      },
      \csname jump@\the\value{jumping}@maxbb\endcsname
      \path (\the\pgf@x,\the\pgf@y);
      \csname jump@\the\value{jumping}@minbb\endcsname
      \path (\the\pgf@x,\the\pgf@y);
    },
  }
}


\def\savepointas#1#2{%
  \expandafter\gdef\csname save@pt@#1\endcsname{#2}%
}

\def\tmk@labeldef#1,#2\@nil{%
  \def\tmk@label{#1}%
  \def\tmk@def{#2}%
}

\tikzdeclarecoordinatesystem{pic}{%
  \pgfutil@in@,{#1}%
  \ifpgfutil@in@%
    \tmk@labeldef#1\@nil
  \else
    \tmk@labeldef#1,(0pt,0pt)\@nil
  \fi
  \@ifundefined{save@pt@\tmk@label}{%
    \tikz@scan@one@point\pgfutil@firstofone\tmk@def
  }{%
  \pgfsys@getposition{\csname save@pt@\tmk@label\endcsname}\save@orig@pic%
  \pgfsys@getposition{\pgfpictureid}\save@this@pic%
  \pgf@process{\pgfpointorigin\save@this@pic}%
  \pgf@xa=\pgf@x
  \pgf@ya=\pgf@y
  \pgf@process{\pgfpointorigin\save@orig@pic}%
  \advance\pgf@x by -\pgf@xa
  \advance\pgf@y by -\pgf@ya
  }%
}
\newcommand\tikzmark[2][]{%
\tikz[remember picture with id=#2] #1;}
\makeatother


\newcommand<>{\boxto}[1]{%
\only#2{\tikz[remember picture with id=#1]
\draw[line width=0.75pt,fill=\fillcol,rectangle,rounded corners]
(pic cs:#1) ++(0.2,-.09) rectangle (0.03,0.29)
;\tikz\node [anchor=base] (#1){};}% <= insertion to store the anchor to be used as based for the annotation
}


\usepackage{color}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}


\lstset{
basicstyle=\ttfamily,
columns=fullflexible,
showstringspaces=false,
commentstyle=\color{gray}\upshape,
escapechar=* % <=  to escape to LaTeX
}

\lstdefinelanguage{XML}
{
  morestring=[b]",
  morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>},
  stringstyle=\color{black},
 identifierstyle=\color{darkblue},
 keywordstyle=\color{cyan},
 morekeywords={xmlns,version,type}% list your attributes here
 }

\begin{document}

\begin{frame}[fragile]{Structure}

\lstset{language=XML}
\begin{lstlisting}
<?xml version="1.0"  encoding="iso-8859-1"?>
*\boxto<1->{a}*<country>*\tikzmark{a}*
    <name> France </name>
    <city> Paris </city>
    <region>
        <name> Nord-Pas De Calais </name>
        <population> 3996 </population>
        <city> Lille </city>
   </region>
   <region>
       *\boxto<2->{b}*<name> Valle du Rhone </name>*\tikzmark{b}*
       <city> Lyon </city>
       *\boxto<2->{c}*<city> Valence </city>*\tikzmark{c}*
       </region>
</country>
\end{lstlisting}
\end{frame}


\end{document}

这使:

在此处输入图片描述

在此处输入图片描述

相关内容