如何在 LaTeX 文档中显示 LaTeX 代码?

发布于 2024-08-04 10:33:56 字数 1047 浏览 2 评论 0 原文

我有一个小问题,我想将 LaTeX 文档的 svn diff 插入到 另一个 LaTeX 文档,目标是显示自修订版 XXX 以来发生的更改。 然而,由于 diff 包含大量 LaTeX 命令,我无法将其直接包含到文档中,因为 LaTeX 会解释它们,而不仅仅是“打印”它们。

今天我的 Makefile 中有这个

DIFF_INFO=diff.info.tex
DIFF_REV=167
diffinfo:
    $(shell echo "\n" > $(DIFF_INFO) )
    $(shell echo "\\section{diff $(DIFF_REV)} \n" >> $(DIFF_INFO) )
    $(shell echo \\\\begin{verbatim} >> $(DIFF_INFO) )
    $(shell svn diff --revision $(DIFF_REV) $(N).tex >> $(DIFF_INFO) )
    $(shell echo \\\\end{verbatim} >> $(DIFF_INFO) )

在 LaTeX 文档的末尾我有这个:

\IfFileExists{diff.info.tex}
{
  \newpage
  \input{diff.info.tex}
}

但这很难失败!

我的下一个想法是编写一个 perl 脚本,用 LaTeX 可以显示的内容替换所有无效字符,但感觉我冒着重新发明轮子的风险,所以我想我可以问是否有人有更好的主意?

如何在文档中包含并显示 LaTeX 代码?

谢谢 约翰


更新: 感谢“未知(谷歌)”逐字指出,它做了我想要的事情。

更新: 我看起来也应该尝试 las3rjock 告诉我们的列表,因为它看起来不错。

更新: 无法让列表在我的情况下工作,我收到一些关于无效字符的奇怪 utf 警告。但逐字记录是有效的,所以这次我将使用这种方式。

I have a little problem where I would like to insert a svn diff of a LaTeX document into
another LaTeX document, the goal is to show what has changed since revision XXX.
However since the diff contains a lot of LaTeX command I can't include it right into the document since LaTeX will interpit them and not just "print" them.

Today I have this in my Makefile

DIFF_INFO=diff.info.tex
DIFF_REV=167
diffinfo:
    $(shell echo "\n" > $(DIFF_INFO) )
    $(shell echo "\\section{diff $(DIFF_REV)} \n" >> $(DIFF_INFO) )
    $(shell echo \\\\begin{verbatim} >> $(DIFF_INFO) )
    $(shell svn diff --revision $(DIFF_REV) $(N).tex >> $(DIFF_INFO) )
    $(shell echo \\\\end{verbatim} >> $(DIFF_INFO) )

And at the end of the LaTeX document I have this:

\IfFileExists{diff.info.tex}
{
  \newpage
  \input{diff.info.tex}
}

But this fails hard!

My next idea is to write a perl script that replaces all invalid chars with something that LaTeX can show, but it feels like I'm risking to reinvent the wheel so I figured that I could ask if someone had a better idea?

How do I include and show LaTeX code in the document?

Thanks
Johan


Update:
Thanks "unknown (google)" for pointing out verbatim, it did what I wanted it to.

Update:
I also looks like I should try that listings that las3rjock told us about since it look kind of nice.

Update:
Could not get listings to work in my case, I get some strange utf warnings about invalid chars. But the verbatim is working so I will use that way this time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

决绝 2024-08-11 10:33:56

我在对另一个答案的评论中赞同 Boojum 的建议,即您使用 列表包。对于 LaTeX 代码列表,我使用在 André Miede 的 中找到的设置classicthesis 包。以下是示例文档 (quine.tex) 及其输出:

\documentclass[12pt,letterpaper]{article}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{color}

% listings settings from classicthesis package by
% Andr\'{e} Miede
\lstset{language=[LaTeX]Tex,%C++,
    keywordstyle=\color{RoyalBlue},%\bfseries,
    basicstyle=\small\ttfamily,
    %identifierstyle=\color{NavyBlue},
    commentstyle=\color{Green}\ttfamily,
    stringstyle=\rmfamily,
    numbers=none,%left,%
    numberstyle=\scriptsize,%\tiny
    stepnumber=5,
    numbersep=8pt,
    showstringspaces=false,
    breaklines=true,
    frameround=ftff,
    frame=single
    %frame=L
}

\begin{document}
\lstinputlisting{quine.tex}
\end{document}

“显示其自己的源代码的
(点击放大)

I second Boojum's recommendation in a comment to another answer that you use the listings package. For LaTeX code listings, I use settings that I found in André Miede's classicthesis package. Here is a sample document (quine.tex) and its output:

\documentclass[12pt,letterpaper]{article}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{color}

% listings settings from classicthesis package by
% Andr\'{e} Miede
\lstset{language=[LaTeX]Tex,%C++,
    keywordstyle=\color{RoyalBlue},%\bfseries,
    basicstyle=\small\ttfamily,
    %identifierstyle=\color{NavyBlue},
    commentstyle=\color{Green}\ttfamily,
    stringstyle=\rmfamily,
    numbers=none,%left,%
    numberstyle=\scriptsize,%\tiny
    stepnumber=5,
    numbersep=8pt,
    showstringspaces=false,
    breaklines=true,
    frameround=ftff,
    frame=single
    %frame=L
}

\begin{document}
\lstinputlisting{quine.tex}
\end{document}

LaTeX document displaying its own source code
(click to enlarge)

雨的味道风的声音 2024-08-11 10:33:56

您可以将一个 verbatim 包包含在 \usepackage{verbatim} 中,并使用 \verbatiminput{filename} 进行访问>。

There' a verbatim package that you can include with \usepackage{verbatim} and access using \verbatiminput{filename}.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文