37

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

topskip
  • 37,020
  • 2
    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. – Matthias Pospiech Nov 21 '11 at 13:56
  • I have slightly extended the question. – topskip Nov 21 '11 at 14:00
  • Do you also want to define custom environments automatically such that <h1> .... </h1> would mean \begin{h1} ... \end{h1}? – percusse Nov 21 '11 at 14:04
  • 8
    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. – Joseph Wright Nov 21 '11 at 14:05
  • @percusse I'd like to keep the standard LaTeX document markup as defined by Lamport. That is: I still like to write \section{...} and have its appearance as defined in the css stylesheet. – topskip Nov 21 '11 at 14:08
  • @Patrick I see, then your problem is confined to making sense out of a CSS file. This looks like doable with a Python or with some other parsing script producing a .cls file. I am not sure if it can be directly read by compiler. – percusse Nov 21 '11 at 14:12
  • 1
    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. – Aditya Nov 21 '11 at 19:48
  • @Aditya It has to be css. Well, I am just thinking if there is such a solution. Css would have the perfect characteristic as being a widely used and adopted standard. – topskip Nov 21 '11 at 21:34
  • 4
    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. – Aditya Nov 21 '11 at 22:14
  • 3
    Here's an example. This uses CSS to style an HTML page like the default LaTeX style. Enjoy! –  Oct 04 '12 at 18:29

3 Answers3

11

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

topskip
  • 37,020
6

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.

enter image description here

Torbjørn T.
  • 206,688
  • 4
    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 ;-) – Johannes_B Sep 08 '15 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). – Torbjørn T. Sep 09 '15 at 07: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. https://raw.githubusercontent.com/mixu/markdown-styles/master/screenshots/montage.png – Robin Thuran Malhotra Sep 09 '15 at 08:10
  • 1
    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.) – Torbjørn T. Sep 10 '15 at 08:59
  • 2
    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". – ShreevatsaR Jan 02 '17 at 01:39
  • That is not LaTeX. It's just HTML, CSS, and JavaScript that makes a website look like a LaTeX document, but that was not the question. The question was about declarative styling of LaTeX documents. – Henri Menke May 29 '20 at 04:38
1

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.

  • 2
    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. – Alan Munn Sep 15 '20 at 00:51
  • Thank you, I edited my answer accordingly. – Eerik Sven Puudist Jul 28 '21 at 17:55