我的 中需要两个代码块beamer
frame
。一个是代码,另一个是每行代码的输出。
我想要揭示每一行代码和相应的输出行。
首先它会显示
code 1 line 1
code 1 line 2
output 1
进而
code 1 line 1
code 1 line 2
code 2 line 1
code 2 line 2
output 1
output 2
我无法使用,semiverbatim
因为我需要代码格式化。
这是我的代码:
\documentclass{beamer}
\usepackage{listings} %for listings of the source code
\lstset{basicstyle=\ttfamily}
\begin{document}
\lstset{language=sh}
\begin{lstlisting}
code 1
code 2
code 3
\end{lstlisting}
\begin{lstlisting}
output 1
output 2
output 3
\end{lstlisting}
\end{document}
有任何想法吗?
我能想到的唯一解决方案是,将每一行代码添加到单独的代码中savebox
,然后为每个代码添加动画savebox
。
答案1
简单组合突出显示代码列表中的文本,同时保持语法突出显示和思维导图 tikzpicture 在 beamer 中 (逐步显示)用一个计数器创造奇迹(我真的应该感谢丹尼尔:)
)。
代码:
\documentclass{beamer}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{listings}
\usetheme{CambridgeUS}
% Daniel's code:
% https://tex.stackexchange.com/questions/55806/tikzpicture-in-beamer/55827#55827
\tikzset{
invisible/.style={opacity=0},
visible on/.style={alt=#1{}{invisible}},
alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
},
}
% Daniel's code:
% https://tex.stackexchange.com/questions/15237/highlight-text-in-code-listing-while-also-keeping-syntax-highlighting/49309#49309
\newcounter{step}
\makeatletter
\newenvironment{btHighlight}[1][]
{\begingroup\tikzset{bt@Highlight@par/.style={#1}}\begin{lrbox}{\@tempboxa}}
{\end{lrbox}\bt@HL@box[bt@Highlight@par]{\@tempboxa}\endgroup}
\newcommand\btHL[1][]{%
\begin{btHighlight}[#1]\bgroup\aftergroup\bt@HL@endenv%
}
\def\bt@HL@endenv{%
\end{btHighlight}%
\egroup
}
\newcommand{\bt@HL@box}[2][]{%
\tikz[#1]{%
\pgfpathrectangle{\pgfpoint{0pt}{0pt}}{\pgfpoint{\wd #2}{\ht #2}}%
\pgfusepath{use as bounding box}%
\refstepcounter{step}
\node[visible on=<\thestep->,anchor=base west,rounded corners, fill=none,outer sep=0pt,inner xsep=0.5pt, inner ysep=2pt, #1]{\usebox{#2}};
}%
}
\makeatother
\lstset{language=sh,
basicstyle=\footnotesize\ttfamily,
keywordstyle=\footnotesize\color{blue}\ttfamily,
moredelim=**[is][\btHL]{|}{|},
}
\begin{document}
\begin{frame}[fragile]{Code}
\begin{columns}
\begin{column}{0.45\textwidth}
\setcounter{step}{0}
\begin{lstlisting}
|code 1|
|code 2|
|code 3|
\end{lstlisting}
\end{column}
\begin{column}{0.45\textwidth}
\setcounter{step}{0}
\begin{lstlisting}
|output 1|
|output 2|
|output 3|
\end{lstlisting}
\end{column}
\end{columns}
\end{frame}
\end{document}
结果:
样式visible on
负责使代码可见:序列是通过计数器实现的step
,计数器在代码突出显示的每个步骤中递增。为了使代码和输出同步后,只需在打开lstlisting
环境之前重置计数器。
免责声明
我使用了columns
环境,因为在这种情况下看起来更好,但没有它解决方案也可以工作。
改进的解决方案:可自定义覆盖代码块和不透明度
要显示一行代码块,事情会稍微复杂一些:我们需要另一个计数器不会增加的环境。因此,我们最终得到了两种类型的分隔符:第一种让计数器增加(您应该将其用于代码块的第一行),第二种保留先前的值,以便当前行显示在代码块第一行的同一覆盖层中(将其用于所有其他代码行)。
为了自定义线条的不透明度,引入了一个特定的键:
\pgfkeys{/tikz/.cd,
set opacity/.initial=0,
set opacity/.get=\opacity,
set opacity/.store in=\opacity,
}
应用于:
\tikzset{
invisible/.style={opacity=\opacity},
visible on/.style={alt=#1{}{invisible}},
alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
},
}
最初,不透明度设置为,0
因此代码神奇地出现:在示例中展示了如何修改它以获得一些半透明效果。
代码:
\documentclass{beamer}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{listings}
\usetheme{CambridgeUS}
% key to customize the opacity
\pgfkeys{/tikz/.cd,
set opacity/.initial=0,
set opacity/.get=\opacity,
set opacity/.store in=\opacity,
}
% Daniel's code:
% https://tex.stackexchange.com/questions/55806/tikzpicture-in-beamer/55827#55827
\tikzset{
invisible/.style={opacity=\opacity},
visible on/.style={alt=#1{}{invisible}},
alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
},
}
% Daniel's code:
% https://tex.stackexchange.com/questions/15237/highlight-text-in-code-listing-while-also-keeping-syntax-highlighting/49309#49309
\newcounter{step}
\makeatletter
\newenvironment{btHighlight}[1][]
{\begingroup\tikzset{bt@Highlight@par/.style={#1}}\begin{lrbox}{\@tempboxa}}
{\end{lrbox}\bt@HL@box[bt@Highlight@par]{\@tempboxa}\endgroup}
\newcommand\btHL[1][]{%
\begin{btHighlight}[#1]\bgroup\aftergroup\bt@HL@endenv%
}
\def\bt@HL@endenv{%
\end{btHighlight}%
\egroup
}
\newcommand{\bt@HL@box}[2][]{%
\tikz[#1]{%
\pgfpathrectangle{\pgfpoint{0pt}{0pt}}{\pgfpoint{\wd #2}{\ht #2}}%
\pgfusepath{use as bounding box}%
\refstepcounter{step}
\node[visible on=<\thestep->,anchor=base west,rounded corners, fill=none,outer sep=0pt,inner xsep=0.5pt, inner ysep=2pt, #1]{\usebox{#2}};
}%
}
\newenvironment{btHighlights}[1][]
{\begingroup\tikzset{bt@Highlights@par/.style={#1}}\begin{lrbox}{\@tempboxa}}
{\end{lrbox}\bt@HLs@box[bt@Highlights@par]{\@tempboxa}\endgroup}
\newcommand\btHLs[1][]{%
\begin{btHighlights}[#1]\bgroup\aftergroup\bt@HLs@endenv%
}
\def\bt@HLs@endenv{%
\end{btHighlights}%
\egroup
}
\newcommand{\bt@HLs@box}[2][]{%
\tikz[#1]{%
\pgfpathrectangle{\pgfpoint{0pt}{0pt}}{\pgfpoint{\wd #2}{\ht #2}}%
\pgfusepath{use as bounding box}%
\node[visible on=<\thestep->,anchor=base west,rounded corners, fill=none,outer sep=0pt,inner xsep=0.5pt, inner ysep=2pt, #1]{\usebox{#2}};
}%
}
\makeatother
\lstset{language=sh,
basicstyle=\footnotesize\ttfamily,
keywordstyle=\footnotesize\color{blue}\ttfamily,
moredelim=**[is][\btHL]{|}{|}, % counter increased
moredelim=**[is][\btHLs]{`}{`}, % counter not increased
}
\begin{document}
\begin{frame}[fragile]{Code}
\begin{columns}
\begin{column}{0.45\textwidth}
\setcounter{step}{0}
\begin{lstlisting}
|code 1 line 1|
`code 1 line 2`
`code 1 line 3`
|code 2 line 1|
`code 2 line 2`
|code 3 line 1|
`code 3 line 2`
`code 3 line 3`
`code 3 line 4`
\end{lstlisting}
\end{column}
\begin{column}{0.45\textwidth}
\setcounter{step}{0}
\tikzset{set opacity=0.2}
\begin{lstlisting}
|output 1|
|output 2|
|output 3|
\end{lstlisting}
\end{column}
\end{columns}
\end{frame}
\end{document}
结果:
答案2
这就是我最终所做的……
\documentclass{beamer}
\usepackage{color}
\usepackage{listings}
\usepackage{setspace}
\usepackage{palatino}
\usepackage{lmodern}
\setbeamertemplate{blocks}[rounded]
\lstset{
basicstyle=\ttfamily\tiny\setstretch{1},
aboveskip=-40pt,
breaklines=true,
frame=leftline
}
\begin{document}
\begin{frame}[fragile, t]{Quotes}
\vspace{4mm} \begin{block}{}<2-> \begin{lstlisting}
str1 = "Hello World!"
print str1
\end{lstlisting} \end{block}
\vspace{-1.5mm} \begin{block}{}<3-> \begin{lstlisting}
str2 = 'Hello World!'
print str2
\end{lstlisting} \end{block}
\vspace{-1.5mm} \begin{block}{}<4-> \begin{lstlisting}
str3 = "Don't do that!"
print str3
\end{lstlisting} \end{block}
\vspace{-1.5mm} \begin{block}{}<5-> \begin{lstlisting}
str4 = 'Don\'t do that!'
print str4
\end{lstlisting} \end{block}
\vspace{1mm} \begin{block}{}<6-> \begin{lstlisting}
str5 = "This is a long line with double quotes here\", here\" and here\" and single quotes here', here' and here'."
print str5
\end{lstlisting} \end{block}
\vspace{1mm} \begin{block}{}<7-> \begin{lstlisting}
str6 = """This is a long line with double quotes here", here" and here" and single quotes here', here' and here'."""
print str6
\end{lstlisting} \end{block}
\vspace{1mm} \begin{block}{}<8-> \begin{lstlisting}
str7 = '''This is a long line with double quotes here", here" and here" and single quotes here', here' and here'.'''
print str7
\end{lstlisting} \end{block}
\begin{block}{}<2-> \begin{lstlisting}
Hello World!
\end{lstlisting} \end{block}
\vspace{-4mm} \begin{block}{}<3-> \begin{lstlisting}
Hello World!
\end{lstlisting} \end{block}
\vspace{-4mm} \begin{block}{}<4-> \begin{lstlisting}
Don't do that!
\end{lstlisting} \end{block}
\vspace{-4mm} \begin{block}{}<5-> \begin{lstlisting}
Don't do that!
\end{lstlisting} \end{block}
\vspace{-1.5mm} \begin{block}{}<6-> \begin{lstlisting}
This is a long line with double quotes here", here" and here" and single quotes here', here' and here'.
\end{lstlisting} \end{block}
\vspace{-1.5mm} \begin{block}{}<7-> \begin{lstlisting}
This is a long line with double quotes here", here" and here" and single quotes here', here' and here'.
\end{lstlisting} \end{block}
\vspace{-1.5mm} \begin{block}{}<8-> \begin{lstlisting}
This is a long line with double quotes here", here" and here" and single quotes here', here' and here'.
\end{lstlisting} \end{block}
\end{frame}
\end{document}