# I have a dream: Using CSS to style a TeX document

I have no clue in LaTeX package developing but I have a dream! Consider following two files:

% A LaTeX file
\documentclass{article}
\usepackage[style.css]{css}

\begin{document}
\begin{abstract}
This is abstract!
\end{abstract}
This is \cls{code}{foo} and that is \cls{code}{bar}.
\end{document}


A simple css.file:

/*style.css*/
abstract{
color:red;
}

.code{
background:blue;
}


I want the package map css rules to its latex counterparts before rendering. In this example:

\documentclass{article}
\usepackage{color}

\begin{document}
{\color{red}
\begin{abstract}
This is abstract!
\end{abstract}
}
This is \colorbox{blue}{foo} and that is \colorbox{blue}{bar}.
\end{document}


I want to know how can I develop a simple package that for beginning provide ability to use color/font-family/font-weight css rules in LaTeX. In next stage I want to add it id/class addressing. Perhaps we can put the source in GitHub and the community could extend it.

Notice that I want to use only CSS syntax as syntax-suger for LaTeX styling.

As an answer please at least provide a minimum solution to set the color of abstract using provided example. Thanks all.

related: CSS based LaTeX formatting?

• Have a look at github.com/yannisl/phd/blob/master/phd.dtx – Yiannis Lazarides Oct 16 '13 at 4:19
• It would be at an innate and undeniable disadvantage from a language standpoint, but it is possible (and perhaps even feasible with a clever use of l3keys). – Sean Allred Oct 16 '13 at 5:00
• The LaTeX3 team have been thinking about how to specify CSS-like information for LaTeX. Life is not as simple as reading CSS files: there are differences in the way TeX and HTML/CSS model things, different requirements, etc. plus CSS has some 'historical baggage' of it's own. – Joseph Wright Oct 16 '13 at 6:08
• Have to admit that while I have some sympathy with the proposal, it's not clear what the actual question here is. This is a TeX Question-and-Answer site, not a place to air general ideas (though try "chat" for that). So unless you can formulate it into an answerable question, I would probably vote to close this as "not a real question" (if that's still allowed as a close reason! They change faster than I can keep up. I think "Not about toast" is still a valid reason.) – Andrew Stacey Oct 16 '13 at 7:20
• There is a sort of proto-approach to css-style structuring, which was introduced to latex 2.09 in its penultimate release -- viz. the \chaptername (and other *name macros); while these were introduced to reduce hassle when writing in non-english languages, they show an approach to writing something that "knows" what it's typesetting. the structure of latex markup doesn't help, but i don't doubt that a css-like class is do-able. – wasteofspace Oct 16 '13 at 9:44

TeX is an extremely powerful typesetting system. While TeX can be used to make toast or control a Mars rover writing complex programs in TeX/LaTeX is generally more difficult than languages like C, Perl, and Python. In order to parse your example CSS

\*style.css*    abstract{
color:red;
}

.code{
background:blue;
}


you would likely have to either make a and . active or process it with regular expressions. As @JosephWright told explained to me in a comment to this answer

We do have regex support in LaTex3, but it's truly 'regular', so many of the things the PCRE can do are out. Performance is also an issue if you want to use it for large-scale work.

I would suggest instead of doing the conversion directly in TeX you use Perl (or a programming language of your choice) to write a program like css2sty which would use a powerful regular expression engine to do the conversion.

• Using lpeg with luatex would not need any external processing (disclaimer: I don't know how to do it though I think it is doable). – cjorssen Oct 16 '13 at 9:27
• Certainly I would not use a regex, but you can imagine setting up the TeX parsing by hand – Joseph Wright Oct 16 '13 at 9:32
• @cjorssen sure, but I don't think that external processing is such a big deal in this case. – StrongBad Oct 16 '13 at 9:32
• Perhaps with LuaTeX one can parse CSS using some Lua module written for that purpose. Or instead the user may be ok specifying the CSS with some LaTeX-like syntax (\abstractcolor{red} or whatever). The other part (changing the TeX instructions generated for a certain LaTeX environment like abstract) seems to be more challenging (or at least, more relevant to this site). (Unfortunately, knowing nothing about LaTeX under the hood, I'm not even sure whether a given LaTeX environment always "expands" to a particular set of TeX instructions, or whether it's something more hacky.) – ShreevatsaR Jan 2 '17 at 1:58