マクロ: LaTeX のコマンドを使用して外部ファイルの {} 内に同じ編集を適用する方法

マクロ: LaTeX のコマンドを使用して外部ファイルの {} 内に同じ編集を適用する方法

いくつかのファイルを編集するためにOverleafを使用しています。

LaTeX で、ファイルの一部を別のファイルにリンクするコマンドを設定することは可能ですか? つまり、ファイル内のすべての内容を{}同じプロジェクトの別の外部ファイルにコピーするということです (すべての変更をそこに適用するので、そのセクションを 2 番目のファイルにコピーして貼り付ける必要はありません)。

たとえば、と.texという名前の 2 つのファイルがあるとします。 の場合、次のようになります。A.texB.texA.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 はサポートしていないと思います。LaTeX はソースを編集しないため、これは実際には LaTeX に関する質問ではありません。

LaTeX の方法では、以下の例のように、共有テキストまたはコマンド テキストを 1 つのファイルにまとめます。

私のコマンド.tex

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

このような小さなフラグメントの場合は、This is a shared text上記のようなコマンドを使用すると便利です。セクション全体などの大きなフラグメントの場合は、別のファイルを使用できます。

するとテキストは1か所にしか表示されなくなり、次A.texB.texように表示されます。

A.テックス

\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}

B.テックス

\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}

共有sec.tex

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

関連情報