Latex 變數和 if 語句顯示或隱藏程式碼

Latex 變數和 if 語句顯示或隱藏程式碼

我想定義一個文檔,在文檔的開頭定義變量,並在文檔中使用它們來定義從程式碼的哪些部分建立pdf。

這是程式碼,我用 ** 編寫,我將如何在 python 中做到這一點。

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\title{A test file}

\begin{document}
\maketitle

*Show_first_line = True*
*Show_second_line = False*

\section{Density functional theory}

*if Show_first_line == True: #Now the following formula should be compiled* 
     $$
     i \hbar \frac{\partial \Phi(\mathbf{r}, \mathbf{R} ; t)}{\partial t}=\left(-\frac{\hbar^{2}}{2 M} \frac{\partial^{2}}{\partial \mathbf{R}^{2}}-\frac{\hbar^{2}}{2 m} \frac{\partial^{2}}{\partial \mathbf{r}^{2}}+V(\mathbf{r}, \mathbf{R})\right) \Phi(\mathbf{r}, \mathbf{R} ; t)
     $$
*else:*
     $$
     i \hbar \frac{\partial \Phi(\mathbf{r}, \mathbf{R} ; t)}{\partial t}=\left( -\frac{\hbar^{2}}{2 m} \frac{\partial^{2}}{\partial \mathbf{r}^{2}}+V(\mathbf{r}, \mathbf{R})\right) \Phi(\mathbf{r}, \mathbf{R} ; t)
     $$

*if Show_second_line == True # In case it is false, nothing should be compiled*
     $$
     \mathrm{M} \gg \mathrm{m} \text { the Born-Oppenheimer approximation }
     $$

$$
i \hbar \frac{\partial \Phi(\mathbf{r}, \mathbf{R} ; t)}{\partial t}=
\left( -\frac{\hbar^{2}}{2 m} \frac{\partial^{2}}{\partial \mathbf{r}^{2}}+V(\mathbf{r}, \mathbf{R})\right) \Phi(\mathbf{r}, \mathbf{R} ; t)
$$

$$
M \ddot{\mathbf{R}}=-\frac{\partial E(\mathbf{R})}{\partial \mathbf{R}} \\
\left(-\frac{\hbar^{2}}{2 m} \frac{\partial^{2}}{\partial \mathbf{r}^{2}}+V(\mathbf{r}, \mathbf{R})\right) \Psi(\mathbf{r} | \mathbf{R})=E(\mathbf{R}) \Psi(\mathbf{r} | \mathbf{R})
$$

\end{document}

答案1

歡迎來到 TeX.SE!以下變化這個答案應該做你想做的事:

\documentclass{article}
\usepackage{environ}
\usepackage{etoolbox}

% The toggles are initially false
\newtoggle{showfirstline}
\newtoggle{showsecondline}

\toggletrue{showfirstline}
\togglefalse{showsecondline}    % useless here

% Uncomment this if you don't want spaces after \end{maybePrint} to be
% discarded (the default is \environfinalcode{\ignorespacesafterend}, which
% causes such spaces to be ignored):
%
%\environfinalcode{}

\NewEnviron{maybePrint}[1]{%
  \iftoggle{#1}{\BODY}{}%
}

\begin{document}

\begin{maybePrint}{showfirstline}
  \[ \frac{\pi^2}{6} = \sum_{n=1}^{+\infty} \frac{1}{n^2}  \]
\end{maybePrint}

\begin{maybePrint}{showsecondline}
  \[ a^2 = b^2 + c^2 \]
\end{maybePrint}

Other text.

\end{document}

螢幕截圖

注意:不要使用$$ ... $$LaTeX 顯示公式,最好使用\[ ... \](參見為什麼 \[ ... \] 優於 $$ ... $$?)。

相關內容