# CSS based LaTeX formatting?

I've done ab bit of web-searching and found nothing. Are there any attempts on using CSS for LaTeX formatting? For example:

body { font-family: Helvetica,Arial,sans-serif; }
section { font-size: 16pt; }
/* or: */
h1 { font-size: 16pt; }


I know that this approach is limited compared to the classic approach, but it could be a nice attempt. So are there any published papers or code pieces?

I'd like to write in the preamble of my LaTeX document: \usestylefile{foo.css} and have the text rendered as written in the given style file (and its linked style files).

• Could you be a bit more specific about the background of your approach? Most of these things can be set up, but none of them so simple as in CSS and in some cases it is not even a good idea to do it the css way. Nov 21, 2011 at 13:56
• Using CSS directly is not likely to be possible. You might, however, be interested in things such as the work on a 'LaTeX3 database' to define relationships between document items. Nov 21, 2011 at 14:05
• Does it have to be css or are you happy to use a css-like configuration. For example, if you define a consistent key-value driven configuration system for your style (for example, as done in ConTeXt), then you can set-up the configuration and let a script (possibly a lua script) read the "css" and convert it into the appropriate tex code. For example head .section {text-color: blue; text-style: bold} could be translated to \setuphead[section][textcolor=blue, textstyle=bold], so you will get the corresponding style in the output. Nov 21, 2011 at 19:48
• OK, let me clarify. Does it has to be CSS tuned to HTML or it can be any valid CSS. The snippet that I posted is actually valid CSS (if you replace text-color by color). So, IMO, all you need to do is come up with a consistent, key-value driven document mode. Then translating CSS style to TeX style is easy. Nov 21, 2011 at 22:14
• Here's an example. This uses CSS to style an HTML page like the default LaTeX style. Enjoy!
– user19535
Oct 4, 2012 at 18:29

## 3 Answers

I'll take the comments as "there is no css-TeX yet".

• I think/hope the key word here is "yet". Feb 4, 2012 at 23:19

Not quite gentlemen. With the combination of markdown, KaTeX, and this (look at the witex example). I think we're super close to realizing a CSS LaTeX.

Here's an example:

1. I start by writing my text in markdown (I usually find it simpler than LaTeX, plus, LaTeXian, my favorite app, is no longer supported on the mac).
2. The markdown is then generated into HTML using markdown-styles, something available via npm.
3. I add KaTeX/MathJax for equation rendering.

I know, I know, a lot of stuff like numbering equations, ToC etc aren't there yet. But even as a proof of concept for tiny college assignments and other things, this is pretty awesome. As an added bonus, you can modify the CSS to customize stuff pretty easily.

• Hi and welcome, can you extend your answer a little bit? Right now it is just Look at the link, but the link could be gone tomorrow. With the content here (properly cited), the problem disappears ;-) Sep 8, 2015 at 20:06
• A couple of questions: which example is that? I can't find the word "witex" anywhere in that page. Second, this doesn't really use LaTeX at all though, does it? MathJaX/KaTeX is not LaTeX, they are math rendering engines that can use a subset of LaTeX syntax, but are otherwise unrelated, LaTeX is much more than math. The question doesn't really have anything to with HTML pages I think, while your example is only about generating HTML (from Markdown). Sep 9, 2015 at 7:42
• I'm only talking about the most common use cases(college assignments, term papers etc). I've said it isn't a replacement. Also, witex is in the screenshot, not in the text. raw.githubusercontent.com/mixu/markdown-styles/master/… Sep 9, 2015 at 8:10
• Ah, I only searched the page, sorry. I won't argue that it is a useful tool, but it still doesn't really answer the question as I see it, as it isn't something you can use in a LaTeX document. If you read the comments you'll see that topskip wants to be able to say \documentclass{article}\usestylefile{file.css}\begin{document}\section{Foo}Text\end{document}, where file.css defines the styling of the \section. While interesting, I don't think this is what was asked for. (By the way, use @username to make sure other commenters are notified of your replies.) Sep 10, 2015 at 8:59
• The question was about putting, in a LaTeX document, something like \usestylefile{foo.css} where foo.css contains styling for the "elements" in the LaTeX document. The content in this answer is interesting, but to repeat the comment on another (deleted converted to comment) answer, "The OP didn't want to know how to style a HTML page but a LaTeX document". Jan 2, 2017 at 1:39

When I need to use CSS, then one option is to write the document as HTML and use some stylesheet to get the LaTeX like style.

My favourite solution is this: https://latex.now.sh/. The outcome is really great (including tables and more advanced stuff) and the HTML will be clean (very little need to add classes or other attributes).

However, this does mean that you will loose most of the benefits of writing in LaTeX except the nice outlook.

• Like the other answers this doesn’t address the question. The idea is to use CSS to apply styles to the document elements of an actual LaTeX document. It has nothing to do with simulating last LaTeX document in HTML. Sep 15, 2020 at 0:51
• Thank you, I edited my answer accordingly. Jul 28, 2021 at 17:55