<p>Actually you can do it and this sort of thing is often done. In particular, C compilers are often written in C itself. Even weirder, the first compiler for C on a new architecture might be written in C through an iterative process called <em>bootstrapping</em>. So, since LaTex is actually a compiler, you can do the same thing.</p>
<p>You work on such things in a series of stages, with only the very first stage needing to be done in another language (or no language other than absolute machine language).</p>
<p>First write a simple thing that will only compile somewhat more complex things, not the full language. Write a somewhat more complex thing that only uses features supported by the simple thing. Then, compile the new thing with the simple thing. Replace the simple thing with the new thing as your basic compiler. Then repeat. Write the V2 new thing using only features of the V1 simple thing and compile it with V1. Replace V1 with V2 and repeat as necessary until Vn is what you want.</p>
<p>In practice two or three iterations might be needed, but usually not more.</p>
<p>In fact, the latest versions of most languages are written this way by writing a compiler for them in the language of the previous version of the language.</p>
<p>There is usually a final stage in such bootstrapping in which a compiler for level n is written in the level n language itself, which only previously had a compiler written in level n-1 language. The compiler then compiles itself, and replaces the earlier version. Once this newest thing is compiled the result replaces the version that compiled it.</p>
<p>I don't know when this was first used, but I'll guess it was during the development of C.</p>
<p>For your application it is much simpler if you already have a LaTex system, write your OS to be compatible with that. Then compile it. Then replace your original OS with this new version. Presumably this new version has its own LaTex compiler.</p>
<p>Alternatively, if you have a Turing Complete language (maybe C) on the new machine that doesn't have a LaTex system, write a compiler for LaTex in C on that new machine and compile it there. You now have a running LaTex compiler on that machine. So, write your LaTex compiler in LaTex and compile it with that first (C) version and then replace the first version with the new one. Now you have a Turning Complete language that you can use to write other programs.</p>
<p>You can perform any <em>computational process</em> on any Turing Machine, but it doesn't need to be easy.</p>
<hr />
<p>And, you should probably read the following pdf if you have any interest in compilers: <a href="https://cs.cmu.edu/%7Erdriley/487/papers/Thompson_1984_ReflectionsonTrustingTrust.pdf" rel="nofollow noreferrer">https://cs.cmu.edu/~rdriley/487/papers/Thompson_1984_ReflectionsonTrustingTrust.pdf</a></p>

CC BY-SA 4.0
2022-01-04T13:10:26.670Z
2022-01-04T14:36:49.983Z
2022-01-04T14:36:49.983Z
Buffy
<p>Somewhere between step 3 and 4 you make an unwarranted leap. TeX is Turing-complete, so you can write a computer <em>in TeX</em> that does anything. However, that computer is not the one you're compiling your LaTeX document on. So you can not write an operating system for your PC in TeX/LaTeX.</p>
<p>That said, most modern LaTeX versions have an OS escape: if you <code>\write18{command}</code>, that command gets executed. That way you can still do file and whatever operations on your machine from LaTeX.</p>

CC BY-SA 4.0
2022-01-06T08:21:57.013Z
A way to improve your answer would be to note that LaTeX will generate a `TeX` document but also one could use plugins and have LaTeX have a side effect of generating assembly or something. In other words if a plugin can have a side effect of creating an error message or log apart from the TeX document then leverage that into outputting the operating system as a side effect.
Guy Coder
CC BY-SA 4.0
2022-01-04T15:03:47.710Z
2022-01-04T15:03:47.710Z
Victor Eijkhout
<p>Can somone point a mistake in my reasoning?</p>
<ol>
<li><p>It is possible to build an operating system.</p>
</li>
<li><p>A Turing complete programming language can by used to program an operating system.</p>
</li>
<li><p>LaTeX is a Turing complete, so you can build an operating system with LaTeX.</p>
</li>
<li><p>You need a LaTeX to compile a tex-document, and that runs in some operating system.</p>
</li>
<li><p>Therefore, by 1, 2 and 3 you can build an operating system with LaTeX but by 4., you can't install it to any machine.</p>
</li>
</ol>
<p>Is there a contradiction or has my reasoning an error?</p>

CC BY-SA 4.0
2022-01-04T21:08:27.187Z
The mistake is in premise 2. A language being Turing-complete has nothing to do with its capacity to program an operating system. In fact, C, used to program most operating systems, [is not even technically Turing-complete](https://cs.stackexchange.com/q/60965/).
user3840170
<self-learning><turing-machines>
Can one build an operating system with LaTeX?
# Can one build an operating system with LaTeX?

(Asked by: username_0 on 2022-01-04)

Can somone point a mistake in my reasoning?

1. It is possible to build an operating system.
2. A Turing complete programming language can by used to program an operating system.
3. LaTeX is a Turing complete, so you can build an operating system with LaTeX.
4. You need a LaTeX to compile a tex-document, and that runs in some operating system.
5. Therefore, by 1, 2 and 3 you can build an operating system with LaTeX but by 4., you can't install it to any machine.

Is there a contradiction or has my reasoning an error?

**Tags:** self-learning, turing-machines

> **Comments:**
> **username_1**: The mistake is in premise 2. A language being Turing-complete has nothing to do with its capacity to program an operating system. In fact, C, used to program most operating systems, [is not even technically Turing-complete](https://cs.stackexchange.com/q/60965/).  

---

## 2 answers

### Answer by username_3 on 2022-01-04. Score: 3 | Accepted answer

Somewhere between step 3 and 4 you make an unwarranted leap. TeX is Turing-complete, so you can write a computer *in TeX* that does anything. However, that computer is not the one you're compiling your LaTeX document on. So you can not write an operating system for your PC in TeX/LaTeX.

That said, most modern LaTeX versions have an OS escape: if you `\write18{command}`, that command gets executed. That way you can still do file and whatever operations on your machine from LaTeX.

> **Comments:**
> **username_4**: A way to improve your answer would be to note that LaTeX will generate a `TeX` document but also one could use plugins and have LaTeX have a side effect of generating assembly or something. In other words if a plugin can have a side effect of creating an error message or log apart from the TeX document then leverage that into outputting the operating system as a side effect.  

---

### Answer by username_2 on 2022-01-04. Score: 1

Actually you can do it and this sort of thing is often done. In particular, C compilers are often written in C itself. Even weirder, the first compiler for C on a new architecture might be written in C through an iterative process called *bootstrapping*. So, since LaTex is actually a compiler, you can do the same thing.

You work on such things in a series of stages, with only the very first stage needing to be done in another language (or no language other than absolute machine language).

First write a simple thing that will only compile somewhat more complex things, not the full language. Write a somewhat more complex thing that only uses features supported by the simple thing. Then, compile the new thing with the simple thing. Replace the simple thing with the new thing as your basic compiler. Then repeat. Write the V2 new thing using only features of the V1 simple thing and compile it with V1. Replace V1 with V2 and repeat as necessary until Vn is what you want.

In practice two or three iterations might be needed, but usually not more.

In fact, the latest versions of most languages are written this way by writing a compiler for them in the language of the previous version of the language.

There is usually a final stage in such bootstrapping in which a compiler for level n is written in the level n language itself, which only previously had a compiler written in level n-1 language. The compiler then compiles itself, and replaces the earlier version. Once this newest thing is compiled the result replaces the version that compiled it.

I don't know when this was first used, but I'll guess it was during the development of C.

For your application it is much simpler if you already have a LaTex system, write your OS to be compatible with that. Then compile it. Then replace your original OS with this new version. Presumably this new version has its own LaTex compiler.

Alternatively, if you have a Turing Complete language (maybe C) on the new machine that doesn't have a LaTex system, write a compiler for LaTex in C on that new machine and compile it there. You now have a running LaTex compiler on that machine. So, write your LaTex compiler in LaTex and compile it with that first (C) version and then replace the first version with the new one. Now you have a Turning Complete language that you can use to write other programs.

You can perform any *computational process* on any Turing Machine, but it doesn't need to be easy.

---

And, you should probably read the following pdf if you have any interest in compilers: [https://cs.cmu.edu/~rdriley/487/papers/Thompson\_1984\_ReflectionsonTrustingTrust.pdf](https://cs.cmu.edu/%7Erdriley/487/papers/Thompson_1984_ReflectionsonTrustingTrust.pdf)


---

