
コメントで尋ねられた質問に自分自身で答えようとしていますこの答え最終的に、Ulrike のコードをベースに、マクロ オーバーレイを認識させる以下のコードを作成しました\balloon
。ただし、リストの前にこのマクロを使用すると、不必要な垂直スペースが発生します。これがどこから来るのかわかりますか?
アップデート
不自然な垂直スペースをより目立たせるために、コードが少し変更され、アニメーションが追加されました。
アップデート2
実際、オーバーレイを認識させるときに重複によるトラブルを避けるためには\balloon
、リストに異なる名前を付ける必要があります。このために、スライド番号 ( \the\beamer@slideinframe
) を使用するのは、特定のフレーム内では十分ですが、ドキュメント全体では十分ではありません。フレーム番号 ( \insertframenumber
) を追加するとうまくいきます。
\documentclass{beamer}
%
\setbeamertemplate{footline}[page number]
%
\usepackage{listings}
\usepackage[foreground]{pagegrid}
\usepackage{tikz}
\usetikzlibrary{tikzmark,fit,calc}
\usetikzmarklibrary{listings}
\lstset{basicstyle=\ttfamily}
\tikzset{%
balloon/.style={%
draw,%
fill=blue!20,%
opacity=0.4,%
inner sep=4pt,%
rounded corners=2pt%
},%
}
%
\makeatletter
\renewcommand\iftikzmark[3]{%
\@ifundefined{save@pt@#1-\the\beamer@slideinframe}{%
#3%
}{%
#2%
}%
}%
\newcommand{\@balloon}[4]{%
\pgfmathtruncatemacro\@firstline{%
#3-1%
}%
\iftikzmark{line-#2-\@firstline-start}{%
\iftikzmark{line-#2-#3-first}{%
\xdef\@blines{%
($ ({pic cs:line-#2-\@firstline-start} -| {pic
cs:line-#2-#3-first})!.5!({pic cs:line-#2-#3-first}) $)%
}%
}{%
\iftikzmark{line-#2-#3-start}{%
\xdef\@blines{%
({pic cs:line-#2-\@firstline-start} -| {pic cs:line-#2-#3-start})%
}%
}{%
\xdef\@blines{(pic cs:line-#2-\@firstline-start)}%
}%
}%
}{%
\xdef\@blines{}%
}%
\foreach \k in {#3,...,#4} {%
\iftikzmark{line-#2-\k-first}{%
\xdef\@blines{%
\@blines (pic cs:line-#2-\k-first)%
}%
}{}%
\iftikzmark{line-#2-\k-end}{%
\xdef\@blines{%
\@blines (pic cs:line-#2-\k-end)%
}%
}{}%
}%
\ifx\@blines\empty%
\else%
\edef\temp{\noexpand\tikz[remember picture,overlay]%
\noexpand\node[fit={\@blines},balloon] (#1) {};}%
\temp%
\fi%
}
%
\newcommand<>{\balloon}[4][code\insertframenumber\the\beamer@slideinframe]{%
\only#5{\@balloon{#2}{#1}{#3}{#4}}%
}
%
\lstnewenvironment{hllisting}[1][]{%
\lstset{name=code\insertframenumber\the\beamer@slideinframe,#1}%
}{%
\lstset{name=}%
}
\makeatother
%
\begin{document}
\begin{frame}[fragile]
\balloon<3>{comment}{5}{6}%
\balloon<4>{comment}{1}{3}%
\begin{hllisting}
1st line of code
2nd line of code
3rd line of code
4th line of code
5th line of code
6th line of code
\end{hllisting}
\end{frame}
\end{document}
答え1
ここでは魔法は使われていません。\tikz[overlay]
一部のフレームに を挿入していますが、他のフレームには挿入していません。 には\tikz[overlay]
サイズがないかもしれませんが、それでもボックスであり、 のようにスペースを挿入できます。つまり、そのようなボックスが害を及ぼさない場所で を使用するか、または、すべてのフレームで同様のボックスを使用することが重要です。たとえば、の定義で の代わりに、David が提案したように を使用するか、または を 使用します。\mbox{}
\tikz[overlay]
\mbox
\alt
\only
\balloon
\documentclass{beamer}
%
\setbeamertemplate{footline}[page number]
%
\usepackage{listings}
\usepackage[foreground]{pagegrid}
\usepackage{tikz}
\usetikzlibrary{tikzmark,fit,calc}
\usetikzmarklibrary{listings}
\lstset{basicstyle=\ttfamily}
\tikzset{%
balloon/.style={%
draw,%
fill=blue!20,%
opacity=0.4,%
inner sep=4pt,%
rounded corners=2pt%
},%
}
%
\makeatletter
\renewcommand\iftikzmark[3]{%
\@ifundefined{save@pt@#1-\the\beamer@slideinframe}{%
#3%
}{%
#2%
}%
}%
\newcommand{\@balloon}[4]{%
\pgfmathtruncatemacro\@firstline{%
#3-1%
}%
\iftikzmark{line-#2-\@firstline-start}{%
\iftikzmark{line-#2-#3-first}{%
\xdef\@blines{%
($ ({pic cs:line-#2-\@firstline-start} -| {pic
cs:line-#2-#3-first})!.5!({pic cs:line-#2-#3-first}) $)%
}%
}{%
\iftikzmark{line-#2-#3-start}{%
\xdef\@blines{%
({pic cs:line-#2-\@firstline-start} -| {pic cs:line-#2-#3-start})%
}%
}{%
\xdef\@blines{(pic cs:line-#2-\@firstline-start)}%
}%
}%
}{%
\xdef\@blines{}%
}%
\foreach \k in {#3,...,#4} {%
\iftikzmark{line-#2-\k-first}{%
\xdef\@blines{%
\@blines (pic cs:line-#2-\k-first)%
}%
}{}%
\iftikzmark{line-#2-\k-end}{%
\xdef\@blines{%
\@blines (pic cs:line-#2-\k-end)%
}%
}{}%
}%
\ifx\@blines\empty%
\else%
\edef\temp{\noexpand\tikz[remember picture,overlay]%
\noexpand\node[fit={\@blines},balloon] (#1) {};}%
\temp%
\fi%
}
%
\newcommand<>{\balloon}[4][code\the\beamer@slideinframe]{%
\alt#5{\@balloon{#2}{#1}{#3}{#4}}{\tikz[overlay,remember picture]\path (0,0);}%
}
%
\lstnewenvironment{hllisting}[1][]{%
\lstset{name=code\the\beamer@slideinframe,#1}%
}{%
\lstset{name=}%
}
\makeatother
%
\begin{document}
\begin{frame}
\tikz[overlay]\node{a};
blblb
\end{frame}
\begin{frame}
blblb
\end{frame}
\begin{frame}[fragile]
\balloon<3>{comment}{5}{6}%
\balloon<4>{comment}{1}{3}%
\begin{hllisting}
1st line of code
2nd line of code
3rd line of code
4th line of code
5th line of code
6th line of code
\end{hllisting}
\end{frame}
\end{document}
答え2
奇妙なことに、私はbeamerとtikzmarkがここで何をしているのかを完全には追跡していませんが、invisibleどうした垂直モードでは、 であっても で\color
あっ\label
ても、\tikzmark
常に扱いにくく、副作用を制御するのが困難です。 いずれの場合も、解決策は、扱いにくいことを行う前に安全に水平モードに入ることです。
もし、するなら
\mbox{\balloon<3>{comment}{5}{6}%
\balloon<4>{comment}{1}{3}}%
すると、リストは 4 つのスライドすべてで同じ位置に表示されます。空の mbox は、少し下に移動する偽の段落を作成しますが、これは固定ポイントであり、すべてのスライドで同じであるため、必要に応じてボックスの後に空白行と負の vspace を使用できます。