TransWikia.com

Macro: How to apply same edits inside a {} in external files using command in LaTeX

TeX - LaTeX Asked on November 29, 2020

I am using Overleaf to edit some files.

Is it possible in LaTeX to set a command that links parts of a file to another? I mean to copy exactly everything inside a {} to another external file under the same project (apply all the changes there so I do not need to copy and paste that section from to the second file)?

For example, I have two .tex files named as A.tex and B.tex. What I expect is if in A.tex I have:

documentclass{article}
newcommandMycommandA[1]{textcolor{red}{#1}}
begin{document}

MycommandA{This is a text}

end{document}

and in B.tex I have is like

documentclass{article}
newcommandMycommandA[1]{textcolor{red}{#1}}
begin{document}

MycommandA{This is text}

end{document}

So I want if I change MycommandA{This is a text} in A.tex to MycommandA{This is a book}, LaTeX automatically applies the same change in B.tex.

One Answer

Many desktop editors support multi-file edits but I don't think Overleaf does, it is not really a latex question as latex never edits its source.

A latex way would be to have anyy shared texts or commands text just in one file, as in the example below.

mycommands.tex

newcommandMycommandA[1]{textcolor{red}{#1}}
newcommandtextA{This is a shared text}

For small fragments like This is a shared text it is convenient to use a command as above. For larger fragments such as an entire section, you can use a separate file.

Then the text only appears in one place, and your A.tex and B.tex can look like

A.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}

B.tex

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}

sharedsec.tex

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

Answered by David Carlisle on November 29, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP