22

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?

Real Dreams
  • 8,298
  • 12
  • 56
  • 78
  • 7
    Have a look at https://github.com/yannisl/phd/blob/master/phd.dtx – yannisl Oct 16 '13 at 04:19
  • 1
    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 05:00
  • 8
    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 06:08
  • @JosephWright I believe that if this is not "unclear what you're asking", then your comment (if elaborated) is the only really reasonable answer. – yo' Oct 16 '13 at 07:09
  • @tohecz Depends on the scope of the requirement: you could imagine parsing a subset of CSS which would make sense, e.g. something akin to the scope of titlesec – Joseph Wright Oct 16 '13 at 07:19
  • 6
    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 07:20
  • @AndrewStacey The question is clear. I asked how to open a file an pars it before rendering? Is it possible to use RegEx in TeX? – Real Dreams Oct 16 '13 at 08:13
  • @PHPst It is possible to use RegEx in LaTeX, have a look at l3regex. However, you seem to contradict yourself. You haven't mention RegEx at all in the question and now you ask about it, and at the same time you say that the question is clear ;) – yo' Oct 16 '13 at 08:37
  • @tohecz OK. I mean in the provided example I am looking for a starting point, an answer may explain how to use RegEx in latex to parse the CSS file. – Real Dreams Oct 16 '13 at 08:41
  • @PHPst Then state the question as precisely as possible, so that it is answerable ;) – yo' Oct 16 '13 at 08:45
  • 1
    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 09:44

1 Answers1

6

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.

StrongBad
  • 20,495
  • 2
    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 09:27
  • 1
    Certainly I would not use a regex, but you can imagine setting up the TeX parsing by hand – Joseph Wright Oct 16 '13 at 09:32
  • 1
    @cjorssen sure, but I don't think that external processing is such a big deal in this case. – StrongBad Oct 16 '13 at 09: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 02 '17 at 01:58