0

My LaTeX-out.tex TeX Code is (single line):

\begin{align*}\alpha + \beta = \gamma\end{align*} Some text here. \begin{align*}\sum^{k}_{i=1} \iota \end{align*} and so on the text. \begin{thebibliography}{000}\bibitem{cqgab7bbabib1}{Aasi J et al LIGO Scientific and VIRGO Collaborations 2014 Constraints on cosmic strings from the LIGO-Virgo gravitational-wave detectors.} \bibitem{cqgab7bbabib2}{Abbott B P et al LIGO and Virgo Collaborations 2016 Astrophysical implications of the binary black hole merger GW150914 L22} \bibitem{cqgab7bbabib3}{Abbott B P et al LIGO and Virgo Scientific Collaboration 2016 Search for transient gravitational waves in coincidence with short duration radio transients during 2007–2013}. \bibitem{cqgab7bbabib4}{Abbott B P et al The LIGO Scientific and Virgo Collaborations 2018 GWTC-1: a gravitational-wave transient catalog of compact binary.}

My Lua Code is (i would like enter(\n) before \begin and enter(\n) before \bibitem):

local file = io.open("LaTeX-out.tex")
local fileedited = io.open("LaTeX-Updated.tex", "w")
local content = file:read "*a"
file:close()
contentmodified = string.gsub(content, [[\begin]], [[\n\begin]])
contentmodified = string.gsub(content, [[\end]], [[\n\end]])
contentmodified = string.gsub(content, [[\bibitem]], [[\n\bibitem]])
contentmodified = string.gsub(content, [[(\begin or \end)]], [[\n%1]])--it's not working **or**
fileedited:write(contentmodified)
fileedited:close()

But it's taking only final find and replace only (like \bibitem) and ignoring \begin and \end. How do solve this?

Balaji
  • 2,282
  • do you want to add a newline or literally the two characters \n ? [[\n\bibitem]] note \n and \b have the same level of quoting here, – David Carlisle Oct 31 '20 at 17:53
  • @DavidCarlisle: I have need a newline and i'm not clear about \n and \b have the same level of quoting – Balaji Oct 31 '20 at 18:00
  • I mean surely you can see if \b in \begin means literally \b then the \n in the same string means literally \n not a newline. See the version in my answer where \n and \b are quoted differently. – David Carlisle Oct 31 '20 at 18:04

1 Answers1

3

You need " not [[ so that \n means newline, and you need to apply your later replacements to the contentmodified result of the earlier changes, not to the original string.

local file = io.open("LaTeX-out.tex")
local fileedited = io.open("LaTeX-Updated.tex", "w")
local content = file:read "*a"
file:close()
contentmodified = string.gsub(content, [[\begin]], "\n\\begin")
contentmodified = string.gsub(contentmodified, [[\end]], "\n\\end")
contentmodified = string.gsub(contentmodified, [[\bibitem]], "\n\\bibitem")
fileedited:write(contentmodified)
fileedited:close()

produces


\begin{align*}\alpha + \beta = \gamma
\end{align*} Some text here. 
\begin{align*}\sum^{k}_{i=1} \iota 
\end{align*} and so on the text. 
\begin{thebibliography}{000}
\bibitem{cqgab7bbabib1}{Aasi J et al LIGO Scientific and VIRGO Collaborations 2014 Constraints on cosmic strings from the LIGO-Virgo gravitational-wave detectors.} 
\bibitem{cqgab7bbabib2}{Abbott B P et al LIGO and Virgo Collaborations 2016 Astrophysical implications of the binary black hole merger GW150914 L22} 
\bibitem{cqgab7bbabib3}{Abbott B P et al LIGO and Virgo Scientific Collaboration 2016 Search for transient gravitational waves in coincidence with short duration radio transients during 2007–2013}. 
\bibitem{cqgab7bbabib4}{Abbott B P et al The LIGO Scientific and Virgo Collaborations 2018 GWTC-1: a gravitational-wave transient catalog of compact binary.}
David Carlisle
  • 757,742