开发者

write newline to file in TeX/LaTeX

I have a latex document that I want to use to write text to a plain text file. I want macros to be expanded, so I can't use \filecontents (which just saves input verbatim to a file)

So far I have

\newwrite\metadatafile
\immediate\openout\metadatafile=\jobname-meta.txt
\immediate\write\metadatafile{Title: \jobname\开发者_Python百科string

Tags: \coursecode.\secnum.\termcode, \coursecode.\termcode, ...

Description:
...}
\immediate\closeout\metadatafile

What comes out in the text file inserts \par tokens instead of my double-newlines, and doesn't have any newlines in it at all. How can I get newlines and have a regular plain text file?


There are two answers: One is to just use a lot of \write statements, one for each line you want written. That is, \write writes a line to the file, including the newline character. So a blank line can be done with just

\write\metadatafile{}

Or you can use the primitive \newlinechar. In plain tex the double-carat macro ^^J is aliased to \newlinechar so you can use that as well. See pages 228 and 348 of The TeXBook.

While I have that book open, I looked up the importance of the \immediate primitive before \write. All usages of \write cited also use \immediate. I've tried without the \immediate and without it things don't get written.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜