5

This question already has an answer here:

I'm new to LaTeX and TeX in general. So far I'm really liking the possibilities it offers for making my documents functional, such as being able to use references to show the page number of another section of my content, or being able to automatically display section headings on each page of that section.

I also agree with LaTeX's core principle that writers shouldn't be responsible for formatting. My day job is in web development, and separation of styling and content is a major practice there. (CSS vs HTML)

Unfortunately, from what I've read so far it seems that LaTeX's real principle is that nobody should be responsible for formatting, and that you should just accept whatever formatting LaTeX gives you. In other words, LaTeX's .tex files appear to be the equivalent of HTML, but I can't find the equivalent of CSS.

I realise you can use .sty files to alter formatting, but these seem clunky and verbose, as well as not very flexible. Looking at the answers to this question it seems the general opinion is that LaTeX doesn't do this well.

For example, if I want to make all my headings in a web page blue I can just do h1 { color: blue; }, but in LaTeX it looks like I have to completely overwrite the builtin command for a heading, duplicating all the existing functionality just to change one property.

I've considered using HTML and CSS as I'm used to them, but they don't handle pages well at all. (I'm not sure if it's even possible to do something basic like display the page number that a reference points to)

So is there a variant of LaTeX that supports a more powerful styling system? (Maybe there's some unholy version of LaTeX that supports CSS stylesheets) Or is there some other typesetting system that would have the page-aware features that LaTeX does. Maybe there's even a rendering engine for HTML+CSS that allows for this? Or maybe I'm misunderstanding something fundamental.

Sorry for the noob question, I've tried doing research but I think I'm approaching this all wrong as nothing I've read has answered my questions.

marked as duplicate by Martin Schröder, Stefan Pinnow, Mensch, user13907, Mico Sep 28 '16 at 22:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Related or even duplicate: tex.stackexchange.com/q/35606 – clemens Sep 28 '16 at 13:25
  • 1
    Keep in mind that LaTeX, or at least TeX, is far far older than HTML or CSS. – John Kormylo Sep 28 '16 at 14:01
  • @JohnKormylo -- and style sheets are older than all of them added together! – Scott Seidman Sep 28 '16 at 14:37
  • 1
    There is much more in LaTeX than displaying contents; for instance, a section title will set up entries for the TOC and the headers – egreg Sep 28 '16 at 17:24
  • 3
    You should keep in mind that customisations to LaTeX only look clunky to you because most classes are not designed with customisation in mind. If, instead of the standard bookor article classes, you look at memoir for instance, you get something which is instantly a lot easier to tailor to your needs. Or maybe you need to write such a class from scratch. But it isn't impossible once you know the things you want to be able to customise. – ienissei Sep 28 '16 at 18:33
11

The equivalent of CSS is the LaTeX class (.cls) or package (.sty) file. It is possible to separate content from presentation almost completely in LaTeX, especially if you define your own commands.

There is an important difference between the two systems, though. CSS controls how a browser displays the contents of an HTML page, with a direct mapping between input file and display. What you see in the browser is the HTML file but displayed according to CSS rule.

LaTeX, by contrast, interprets typesetting commands in the input file to produce a DVI or PDF that may differ widely from the structure of the input file, including (as egreg notes in the comments) automatically generated text like table of contents, context-sensitive quotation marks, line drawings, etc. LaTeX only appears to be a markup language; really it is a macro-expansion language specifically for print typesetting.

Possibilities for customization with LaTeX

In the example below I show about one percent of how much you can change the appearance of a single input file by changing the class. Instead of writing separate class files, here I use class options to make the changes.

(Please note that I am not saying the things I demonstrate are aesthetically good or representing good programming practices! I have not tried to provide a simple example.)

Comparison

The rainbow title is a good example for comparison between LaTeX and CSS: I am not just setting a color property of a document element as one would in CSS, but I actually use loops, conditionals, branching, and recursion to iterate over the letters of the title, increment a counter, select a color from a kind of lookup table based on the counter, and print the letter in that color.

Nevertheless, the input file contains nothing about color, and with different command definitions you could create variable output.


Output using \documentclass[rainbowtitle]{example1}:

enter image description here

Output using \documentclass[palatino,notranslation]{example1}:

enter image description here

Input file: example.tex

\documentclass[rainbowtitle]{example1}
% Available class options:                                                                                                                                                                                                         
  %  palatino -- use palatino fonts with different arrangement of styles                                                                                                                                                           
  %  notranslation -- hide the translation in poemtranslation environment                                                                                                                                                          
  %  rainbowtitle -- display the title in rainbow colors!                                                                                                                                                                          

\title{Cascading Styles in \LaTeX}

\begin{document}
\maketitle

\section{Introduction}

This is an example of a \LaTeX{} file whose appearance can be changed drastically by using a different class.

\section{Custom Commands}

The difference between \code{article} and \code{memoir} classes is striking enough, not to mention \code{beamer}.
\keyword{Packages} or (in older terminology) style files can be used with multiple classes to change only one aspect, while the \keyword{class} file is more comprehensive, including packages.

\section{Custom Environments}

\begin{notabene}
Please note that this list is not exhaustive.
\end{notabene}

\begin{itemize}
\item \code{article}
\item \code{report}
\item \code{book}
\item \code{memoir}
\end{itemize}

Here is another example of a custom environment that can be completely redefined.

\begin{poemtranslation}
\begin{original}
Uno, dos, tres, cuatro, \eol
cinco, seis, siete, \eol
nuevo, diez. 
\end{original}
\begin{translation}
One, two, three, four \eol
five, six, seven, \eol
eight, nine.
\end{translation}
\end{poemtranslation}

\end{document}

Class file with options: example1.cls

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{example1}[2016/09/28 Example 1 of changeable formatting]
\LoadClass{article}

% Available class options:
  %  palatino -- use palatino fonts with different arrangement of styles
  %  notranslation -- hide the translation in poemtranslation environment
  %  rainbowtitle -- display the title in rainbow colors!

\newif\iftranslation
\translationtrue
\DeclareOption{notranslation}{\translationfalse}

\newif\ifpalatino
\palatinofalse
\DeclareOption{palatino}{\palatinotrue}

\newif\ifrainbowtitle
\rainbowtitlefalse
\DeclareOption{rainbowtitle}{\rainbowtitletrue}

\ProcessOptions\relax


\ifpalatino
  % We can change all the font styles around with one switch.
  \RequirePackage{newpxtext,newpxmath}
  \let\textsf\textbf
  \let\oldscshape\scshape
  \let\scshape\itshape
  \let\itshape\oldscshape
\else
  \RequirePackage{Alegreya, AlegreyaSans}
\fi
\RequirePackage[T1]{fontenc}
\RequirePackage[utf8]{inputenc}
\RequirePackage{microtype}

% Compact lists
\RequirePackage{enumitem}
\setlist{noitemsep}

% We can change the section headings with one command
\RequirePackage{sectsty}
\allsectionsfont{\scshape\color{blue}}

% We can define custom commands and do anything we like with them.
% Some of these are redefined by the font switch above.
\RequirePackage{xparse}
\RequirePackage{xcolor}

\NewDocumentCommand{\code}{}{\textsf}

% Rainbowtitle option puts each letter of title in different 
% rainbow color
\ifrainbowtitle
  \def\scando{}
  \def\scan#1{\scanA#1\end} % thanks to wipet for this code
  \def\scanA#1{\ifx\end#1\else\scando#1\expandafter\scanA\fi}

  \newcount\rainbowcolor
  \def\spectrum#1{%
    \textcolor{\getcolor{\the\rainbowcolor}}{#1}\thinspace
    \ifnum\rainbowcolor > 4
      \rainbowcolor=0
    \else
      \advance\rainbowcolor by 1
    \fi
  }

  \def\getcolor#1{\ifcase#1 red\or orange\or yellow\or green\or blue\or violet\fi}

  \def\rainbow#1{%
    \rainbowcolor=0
    \let\scando\spectrum
    \expandafter\expandafter\scan{#1}
  }

  \RenewDocumentCommand{\maketitle}{}{%
    \begin{center}
    \Huge \rainbow{\@title}
    \end{center}
    \bigskip
  }
\fi

% Fancy underlining
\RequirePackage{ulem}
\NewDocumentCommand{\keyword}{ m }{\textcolor{magenta}{\uwave{#1}}}

% Redefinable environment, including font style and spacing
\NewDocumentEnvironment{notabene}{}
  {\itshape}
  {\par\addvspace{2ex}}

% Another customizable environment, for parallel poetic text and translation
% With one switch we can turn off one of the columns
\RequirePackage[spanish,english]{babel}
\RequirePackage{multicol}

\iftranslation
  \NewDocumentEnvironment{poemtranslation}{}
    {\footnotesize\raggedright
     \begin{multicols}{2}}
    {\end{multicols}}
  \NewDocumentEnvironment{original}{}
    {\selectlanguage{spanish}
     \color{red}
     \begin{verse}}
    {\end{verse}}
  \NewDocumentEnvironment{translation}{}
    {\columnbreak
     \color{blue}
     \begin{verse}}
    {\end{verse}}
\else
  \NewDocumentEnvironment{poemtranslation}{}
    {\color{violet}}
    {}
  \NewDocumentEnvironment{original}{}
    {\selectlanguage{spanish}
     \begin{verse}}
    {\end{verse}}
  \RequirePackage{environ}
  \NewEnviron{translation}{}[]
\fi

% This could be redefined to, say, `&` if you wanted to use reledmac/reledpar.
\NewDocumentCommand{\eol}{}{\\}

\endinput

Not the answer you're looking for? Browse other questions tagged or ask your own question.