# Writing LaTeX from Julia

**URL:** <https://discourse.julialang.org/t/writing-latex-from-julia/50187>\
**Category:** General Usage\
**Tags:** package\
**Created:** [November 15, 2020, 3:04pm UTC](https://discourse.julialang.org/t/writing-latex-from-julia/50187 "2020-11-15T15:04:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)\
**Post date:** [November 15, 2020, 3:04pm UTC](https://discourse.julialang.org/t/writing-latex-from-julia/50187/1 "2020-11-15T15:04:29Z")

</div>

I’m trying to write LaTeX (which I’m sure most of you know involves a lot of backslashes, e.g. `\dfrac{3}{5}` to create a fraction with numerator 3 and denominator 5) to a text file. By a DuckDuckGo search I found that sed isn’t very LaTeX friendly in itself, so to insert that example piece of LaTeX to say line 19 I’d need to run:

```julia
sed -i "19i\\\dfrac{3}{5}" file.tex

```

I’ve tried using `run` to run this command in Julia (i.e. I’ve run:

```julia
run(`sed -i "19i\\\dfrac{3}{5}" file.tex`)

```

, and what I get is: `dfrac{3}{5}` being added to line 19 of file.tex. I’ve tried with fewer backslashes (1 backslash and 2 backslashes have been tried) and it doesn’t fix this error.

Is there a way around this issue?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [November 15, 2020, 3:10pm UTC](https://discourse.julialang.org/t/writing-latex-from-julia/50187/2 "2020-11-15T15:10:21Z")

</div>

There’s a package called latexstrings.jl that help you with this.

---

<div class="post-metadata">

**Author:** ![malacroi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malacroi/32/19745_2.png) [@malacroi](https://discourse.julialang.org/u/malacroi)\
**Post date:** [November 15, 2020, 3:13pm UTC](https://discourse.julialang.org/t/writing-latex-from-julia/50187/3 "2020-11-15T15:13:48Z")

</div>

My best guess would be that for `sed` to output a `\`, the slash needs to be escaped, with an extra '', but you need to escape each of those `\`s in your julia string, for a total of 4.

---

<div class="post-metadata">

**Author:** ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)\
**Post date:** [November 15, 2020, 3:20pm UTC](https://discourse.julialang.org/t/writing-latex-from-julia/50187/4 "2020-11-15T15:20:40Z")

</div>

I’ve looked at the usage info and I’m guessing using:

```julia
string = L"\\dfrac{3}{2}"
run(`sed -i "19i $string" file.tex`)

```

is what you had in mind. While for this case, this works as this is meant to be in an equation and doing this causes `$\dfrac{3}{2}$` to be added to file.tex, but not all the LaTeX I want to add will be inline equations, so it doesn’t seem like this is a general solution unless I’m wrong in my usage of it.

---

<div class="post-metadata">

**Author:** ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)\
**Post date:** [November 15, 2020, 3:21pm UTC](https://discourse.julialang.org/t/writing-latex-from-julia/50187/5 "2020-11-15T15:21:15Z")

</div>

Tried four backslashes before `dfrac` and I get the same result.

---

<div class="post-metadata">

**Author:** ![fusion809](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fusion809/32/6080_2.png) [@fusion809](https://discourse.julialang.org/u/fusion809)\
**Post date:** [November 15, 2020, 4:43pm UTC](https://discourse.julialang.org/t/writing-latex-from-julia/50187/6 "2020-11-15T16:43:58Z")

</div>

@simeonschaub provided me with a general solution to this problem at [Write to a particular line in a file](https://discourse.julialang.org/t/write-to-a-particular-line-in-a-file/50179). Here’s the function I’m going to use to edit my LaTeX file and insert my LaTeX code (contained within `string`):

```julia
function skiplines(io::IO, n)
    i = 1
    while i <= n
       eof(io) && error("File contains less than $n lines")
       i += read(io, Char) === '\n'
    end
end

function insertLine(file::String, string::String, lineNr::Integer)
    f = open(file, "r+");
    skiplines(f, lineNr);
    skip(f, -1)
    mark(f)
    buf = IOBuffer()
    write(buf, f)
    seekstart(buf)
    reset(f)
    print(f, string);
    write(f, buf)
    close(f)
end

```

For the aforementioned example I’d use the code:

```julia
insertLine("file.tex", "\\dfrac{3}{2}", 19)

```

to call this function.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 16, 2020, 8:54am UTC](https://discourse.julialang.org/t/writing-latex-from-julia/50187/7 "2020-11-16T08:54:18Z")

</div>

Perhaps if you provided some context, you could get a more general solution.

Usually, LaTeX and similar output is easiest to _generate_ from Julia, instead of replacing strings in existing files. You can use a some kind of template for this, or alternatively just write out a tiny snippet like

```nohighlight
\dfrac{3}{2}

```

into a file and `\input` it in your LaTeX source.
