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

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

我正在制作 Beamer 演示文稿。如何在演示文稿中显示 LaTeX 代码?

答案1

无论是简单地在verbatim环境中,还是更复杂的列表包裹。

答案2

如果您希望输入的 LaTeX 代码与其对应的输出并排显示(左/右或上/下),请使用 showexpl.sty。这是一个非常酷的包。

有两种模式:

  1. 输入代码已插入到您的文档中,或者
  2. 输入代码从外部文件导入。

编辑1:

下图显示了 showexpl.sty 的运行情况:

替代文本

并给出相应的代码如下: 注意:我在代码中给出了一些注释,以使它更清楚。

\documentclass[final,dvipsnames]{article}
\usepackage[hmargin=70mm]{geometry}
\usepackage{xcolor}
\usepackage{bera}
\usepackage{pstricks-add}

\usepackage{caption}
\captionsetup{%
   margin=0em,
   font={scriptsize,rm},
   labelfont={color=Maroon,bf},
   justification=justified,%default: RaggedRight. Other options: justified, centering
   labelsep=quad%default:colon. Options: period, space, quad, newline
}
\usepackage{showexpl}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\SX@codeInput}{xleftmargin=0pt,xrightmargin=0pt}{}
  {\typeout{***Successfully patched \protect\SX@codeInput***}}
  {\typeout{***ERROR! Failed to patch \protect\SX@codeInput***}}
\makeatother
\lstset{%
    breaklines=true,%default : false 
  breakindent=10pt,%default: 20pt 
    linewidth=\linewidth,%default : \linewidth,
    basicstyle=\ttfamily\tiny,% cannot take arguments
    keywordstyle=\color{Blue}\sffamily\bfseries,                                
    identifierstyle=\color{Black},                                      
    commentstyle=\color{OliveGreen}\itshape,                                    
    stringstyle=\rmfamily,                                                      
    showspaces=false,%default false
    showstringspaces=false,%default: true
    backgroundcolor=\color{Yellow!30},
    frame=single,%default frame=none 
    rulecolor=\color{Red},  
    % the following must be defined to make hacking work.
    framerule=0.4pt,%expand outward 
    framesep=3pt,%expand outward
    xleftmargin=3.4pt,%to make the frame fits in the text area. 
    xrightmargin=3.4pt,%to make the frame fits in the text area. 
    tabsize=2%,%default :8 only influence the lstlisting and lstinline.
}
\lstset{%
    %explpreset below will affect LTX only.
    explpreset={%
                            language={[LaTeX]TeX},
                            captionpos=t,
                            aboveskip=2\fboxsep,                        
                            pos=b,
                            vsep=\fboxsep%vertical space between formatted and input.
                         }%
}

\newcommand\mywidth{0.5\linewidth}


\newcommand\dummy{%
Thank you to those who never get tired of helping me.
Thank you to those who never get tired of helping me.
Thank you to those who never get tired of helping me.
Thank you to those who never get tired of helping me.
Thank you to those who never get tired of helping me.
}


\begin{document}
\begin{LTXexample}[caption=\dummy]
\dummy
\begin{enumerate}
\item $\displaystyle \lim_{x\to 8}\frac{1}{(x-8)^2}=+\;\rotatebox{90}{$8$}$
\item $\displaystyle \lim_{x\to 7}\frac{1}{(x-7)^2}=+\;\rotatebox{90}{$7$}$
\item $\displaystyle \lim_{x\to 6}\frac{1}{(x-6)^2}=+\;\rotatebox{90}{$6$}$
\end{enumerate}
\end{LTXexample}
\end{document}

答案3

如何使用 verbatiminput?如果您的文件中含有 LaTeX 示例?

\usepackage{verbatim}
\verbatiminput{ my_test.tex }

相关内容