一文搞懂Overleaf(Latex)中的Environment和\newenvironment命令
文章目錄
- 什么是Environment
- 自定義Environment(\newenvironment命令)
- 自定義Environment傳遞參數(shù)
- 定義單個參數(shù)
- 定義多個參數(shù)
- 重寫現(xiàn)有Environment
- 實(shí)戰(zhàn):定義摘要
- 參考資料
什么是Environment
在Latex中,\begin{sth} 內(nèi)容 \end{sth} 這種類型的都是environment。他描述了內(nèi)容應(yīng)該如何展示。
例如:
\begin{center} 這個一個系統(tǒng)定義好的Environments \end{center}center這個environment定義應(yīng)該將其內(nèi)容居中展示。
自定義Environment(\newenvironment命令)
定義好的environment并不能滿足人們的需求,比如不同學(xué)校的摘要要求字體,格式等都不一樣,所以需要自定義environment
通過\newenvironment命令可以自定義environment,格式為 \newenvironment{name}{pre}{after},其中name表示environment的名稱,pre表示內(nèi)容前的內(nèi)容,after表示內(nèi)容后的內(nèi)容。 例如
\newenvironment{myenv}{文檔前面的內(nèi)容\par}{文檔后面的內(nèi)容}\begin{myenv} 文檔的內(nèi)容\par \end{myenv}
可以看到,使用自定義environment myenv,他會自動在文檔前面和后面增加指定的內(nèi)容。
\par 是指切換下一段,類似換行。
除了指定文本內(nèi)容外,還可以在environment中使用environment,例如
\newenvironment{boxed}{\begin{center}\begin{tabular}{|p{0.9\textwidth}|}\hline\\}{ \\\\\hline\end{tabular} \end{center}} %--------------------------------------------------boxed之前的內(nèi)容\begin{boxed} This is the text formatted by the boxed environment \end{boxed}boxed之后的內(nèi)容上述內(nèi)容等價于:
文檔之前的內(nèi)容\begin{center}\begin{tabular}{|p{0.9\textwidth}|}\hline\\ 文檔內(nèi)容 \\\\\hline\end{tabular} \end{center}文檔之后的內(nèi)容到這里,其實(shí)就很容易看出,environment的本質(zhì)為:將\begin{name}替換為pre,將\end{name}替換為after
\newenvironment{name}{pre}{after}\begin{name} \end{name}自定義Environment傳遞參數(shù)
\newenvironment{name}[參數(shù)個數(shù)][首參數(shù)默認(rèn)值]{pre}{after}
定義單個參數(shù)
\newenvironment{myenv}[1][默認(rèn)值]{參數(shù):#1 \par}{}\begin{myenv} 內(nèi)容 \end{myenv}\begin{myenv}[參數(shù)1] 內(nèi)容 \end{myenv}
該實(shí)例中,定義了1個參數(shù),參數(shù)默認(rèn)值為默認(rèn)值。在pre中可以使用#1獲取參數(shù)。after中不能獲取參數(shù)。
定義多個參數(shù)
\newenvironment{myenv}[2][默認(rèn)值]{參數(shù):#1 \par 參數(shù)2:#2 \par}{}\begin{myenv}[參數(shù)1]{參數(shù)2} 內(nèi)容 \end{myenv}\begin{myenv}{參數(shù)2} 內(nèi)容 \end{myenv}
當(dāng)有多個參數(shù)時,第一個參數(shù)使用[]傳遞,后面的參數(shù)使用{}傳遞
重寫現(xiàn)有Environment
和前面一樣,只需要將newenvironment改為renewenvironment 即可
實(shí)戰(zhàn):定義摘要
要求:
- 標(biāo)題:小二號黑體字居中
- 正文:為小四號宋體,行距20磅,首行縮進(jìn)二個字符
參考資料
官方文檔:https://www.overleaf.com/learn/latex/Environments
【LaTeX入門】03、設(shè)置字體相關(guān)命令: https://blog.csdn.net/xiazdong/article/details/8892070
總結(jié)
以上是生活随笔為你收集整理的一文搞懂Overleaf(Latex)中的Environment和\newenvironment命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 方正平台(企业级应用的开发平台)
- 下一篇: YOLOv5损失函数定义