楼主: oliyiyi
1918 1

Why LaTeX beamer needs fragile when using verbatim [推广有奖]

版主

已卖:2994份资源

泰斗

1%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
7
论坛币
84105 个
通用积分
31671.0967
学术水平
1454 点
热心指数
1573 点
信用等级
1364 点
经验
384134 点
帖子
9629
精华
66
在线时间
5508 小时
注册时间
2007-5-21
最后登录
2025-7-8

初级学术勋章 初级热心勋章 初级信用勋章 中级信用勋章 中级学术勋章 中级热心勋章 高级热心勋章 高级学术勋章 高级信用勋章 特级热心勋章 特级学术勋章 特级信用勋章

楼主
oliyiyi 发表于 2016-1-10 19:43:23 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

While preparing my slides for the upcoming Dutch LaTeX course I’ll be giving (if you’re interested, contact me) for our ACM Student Chapter I got puzzled by the factbeamer requires me to use [fragile] as the option to every slide containing verbatim text. I decided to figure it out.

First, what does fragile mean? It has to do with expansion and execution: TeX does these at the same time, first it reads an token, expands it so that only low-level tokens are left and then executes it. But this cycle isn’t always followed: for instance when moving text around this behaviour changes.

When will TeX move text? For instance when creating a table of contents: the \section{}‘s are scattered throughout your document, but in the end they all end up in your table of contents. When expanding and executing a \section{} command it will write the current section’s title and number to a .toc file. A second pass then fills in the table of contents, based on the data that was written in the first pass.

A command that is not fragile is called robust by the way.

So, why is this a problem? The meaning of code changes during TeX’s process and only when the typesetting is done, the actual meaning can be determined. So when writing to data to a file, some expansions must not occur, because they are dependent on the current situation (in the table of contents case: when we’re typesetting the table of contents, as opposed to typesetting the actual\section{}‘s). Examples of these are:

  • commands with optional arguments
  • line breaks
  • footnotes
  • inline math

How should I deal with these issues? Just prefix it with\protect, your fragile command is now protected. But this is not the point of this post. I was interested in whybeamer so desperately needs the [fragile] option when typesetting slides containing verbatim text.

Section 12.9 of the manual states the following on the issue:

[size=1em]If you wish to use a {verbatim} environment in a frame, you have to add the option [fragile] to the {frame} environment. In this case, […] and the \end{frame} must be alone on a single line. Using this option will cause the frame contents to be written to an external file and the read back.

Not really a satisfactory answer, it only states what we already knew about fragile commands and their usage. And it signifies some of the weird things going on withbeamer: somehow the whitespace in front of\end{frame} got a special meaning. Rather scary, don’t you think?

But we’re not interested in TeX voodoo, we want an explanation for [fragile]. A closer inspection of Section 8.1 which defines {frame} reveals the following:

[size=1em]If a frame contains fragile text, different internal mechanisms are used to typeset the frame to ensure that inside the frame the character codes can be reset. The price of switching to another internal mechanism is that either you cannot use overlays or an external file needs to be written and read back (which is not always desirable).

[size=1em]In detail, the following happens when this option is given for normal (pdf)LaTeX: The contents of the frame is scanned and then written to a special file named ⟨jobname⟩.vrb or, if a label has been assigned to the frame, ⟨jobname⟩.⟨current frame number⟩.vrb. Then, the frame is started anew and the content of this file is read back. Since, upon reading of a file, the character codes can be modified, this allows you to use both verbatim text and overlays.

[size=1em]To determine the end of the frame, the following rule is used: The first occurence of a single line containing exactly \end{⟨frame environment name⟩} ends the frame. The ⟨environment name⟩ is normally frame, but it can be changed using the environment option. This special rule is needed since the frame contents is, after all, not interpreted when it is gathered.

Now we’re getting somewhere: beamer‘s internal magic is really, well, quite magical. It fiddles with character codes, the stuff that makes TeX work like you expect it to. But TeX is highly adaptable, no one imposes that your syntax should be \command{parameter}, it can easily be changed to |code[parameter]. There are two cases I can come up with right now where this is a good idea: verbatim text and packages performing magic.beamer is one of the latter, but it should work together with verbatim text as well, hence the hassle.

An example of beamer‘s use of character codes are the greater-than and less-than symbol: they are active characters for beamer. They are for instance used when changing the mode of the presentation (something you do when you’re both creating a presentation and handouts). Now verbatim text also changes the (local) meaning of these characters. And this is exactly why you need the [fragile] option.

A downside of this construction is an extended build time: all fragile frames are written to a file (you can read these files when building your document, that’s how I found out in the first place) and then read again. But thesingleslide option seems to eliminate this for frames without \pause‘s or other kinds of overlays. Hurray!


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:fragile BEAMER Agile needs Using interested execution contact decided course

缺少币币的网友请访问有奖回帖集合
https://bbs.pinggu.org/thread-3990750-1-1.html

沙发
fumingxu 发表于 2016-1-10 19:47:57

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-26 22:21