# 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:** 1\
**Showing post:** 6

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

---

_[View the full topic](https://discourse.julialang.org/t/writing-latex-from-julia/50187)._
