巨集:如何使用 LaTeX 中的指令在外部檔案的 {} 內套用相同的編輯

巨集:如何使用 LaTeX 中的指令在外部檔案的 {} 內套用相同的編輯

我正在使用 Overleaf 編輯一些文件。

在 LaTeX 中是否可以設定將文件的一部分連結到另一個文件的命令?我的意思是將 a 中的所有內容完全複製{}到同一項目下的另一個外部文件中(應用所有更改,這樣我就不需要將該部分複製並粘貼到第二個文件中)?

例如,我有兩個.tex名為A.tex和 的檔案B.tex。我期望的是如果A.tex我有:

\documentclass{article}
\newcommand\MycommandA[1]{\textcolor{red}{#1}}
\begin{document}

\MycommandA{This is a text}

\end{document}

B.tex有就像

\documentclass{article}
\newcommand\MycommandA[1]{\textcolor{red}{#1}}
\begin{document}

\MycommandA{This is text}

\end{document}

所以我希望如果我更改為\MycommandA{This is a text},LaTeX 會自動套用相同的變更。A.tex\MycommandA{This is a book}B.tex

答案1

許多桌面編輯器支援多文件編輯,但我認為 Overleaf 不支持,這實際上並不是乳膠問題,因為乳膠從不編輯其原始碼。

一種乳膠方式是將任何共享文字或命令文字僅放在一個文件中,如下例所示。

mycommands.tex

\newcommand\MycommandA[1]{\textcolor{red}{#1}}
\newcommand\textA{This is a shared text}

對於像這樣的小片段,This is a shared text使用上面的指令很方便。對於較大的片段(例如整個部分),您可以使用單獨的檔案。

那麼文本只出現在一個地方,而你的A.texandB.tex可以看起來像

特克斯

\documentclass{article}
\input{mycommands}
\begin{document}

\section{Intro for A}% not shared

\MycommandA{\textA}% shared

\MycommandA{Text just in A but using the shared command}

\input{sharedsec}% shared
\end{document}

特克斯

\documentclass{report}
\input{mycommands}
\begin{document}

\section{Intro for B}% not shared

\MycommandA{\textA}% shared

Some text just in B.

\input{sharedsec}% shared
\end{document}

共享安全文件

\section{Something}
This text just appears once in the source but appears in A and B

相關內容