3

Is there a generally applicable technique which will, for any tag/environment used in a LaTeX document, allow the default styling of the contents of instances of that tag/environment to be tweaked in the preamble?

  • For instance, if I decided that all instances of \emph{} in the document should have their contents rendered not only italicised (a common default) but also bold, then what would I do?

  • Similarly, if I decided that all instances of \begin{verbatim}...\end{verbatim} should have their contents rendered not only in a monospaced font (a common default) but also on a grey background, then what would I do?

But more importantly than simply explaining how to handle these two example cases, please can you outline a procedure a LaTeX user can follow in all cases in order to be able to change the styling of any given tag/environment.

  • 3
    These are really very different questions. There is no procedure that can work in all cases. – egreg Jul 04 '12 at 17:08
  • Ideally, I'd be after something that works like using the <style> element in the <head> section of an HTML document. This would be a generally applicable single-step solution. However, if there is no generally applicable single-step solution, that does not mean there is no generally applicable solution. How, @egreg (and others reading this), if you did not know how to style a tag/environment, would you approach the problem of first finding out how to style it and second styling it using the preamble or some other document-wide mechanism? –  Jul 04 '12 at 17:28
  • 1
    Just one example: \section has to do much more than a <head> tag in HTML, where, for instance, there's no problem with page breaking, widows and orphans. Comparing (La)TeX and HTML is really wrong. – egreg Jul 04 '12 at 17:42
  • I don't see any similarity between \section in LaTeX and <head> in HTML. However, the preamble in LaTeX is comparable to <head> in HTML. –  Jul 04 '12 at 17:45
  • 1
    OK, wrong example. But still there is no similarity between LaTeX and HTML, other than both use a kind of markup. – egreg Jul 04 '12 at 17:48
  • 1
    That's a pretty big similarity! Anyhow, I provided the analogy merely in case it helped anyone here see what I was getting at, not for any other reason. If it didn't help you, that's a pity, but it may yet help someone else. My question still stands. –  Jul 04 '12 at 17:50
  • I guess the comparison is \section to <h1>. LaTeX wasn't designed to be easily stylable by authors. On the other hand ConTeXt is (as far as I know – I never really used it) much easier to style in this way. – Caramdir Jul 04 '12 at 18:33
  • 1
    @egreg: In some sense one could argue that from a users point of view, LaTeX is similar to plain HTML 4 (without CSS (and hence <style>), JavaScript, etc.). Except of course that LaTeX automates many tasks (toc, ...). – Caramdir Jul 04 '12 at 18:38
  • @Caramdir, if not for authors, then for whom was LaTeX designed to be easily styled? –  Jul 04 '12 at 18:41
  • @sampablokuper You're presuming that LaTeX was designed to be easily styled. Probably better would be to assume that it was designed to be flexible. The "ease", I guess, would be contained in whatever interface the package/macro writers are able to provide to the author. (I'm just a rookie, so take that with a grain of salt) – Scott H. Jul 04 '12 at 19:01
  • @ScottH. no, I was picking up on Caramdir's comment that "LaTeX wasn't designed to be easily stylable by authors", which begs the question. –  Jul 04 '12 at 20:08
  • @sampablokuper: Partially the idea is that users should not be able to alter styles easily. Getting good typography is not easy and (so the idea goes) something best left to the experts. If you want to do something different, you should use a corresponding class or package. (Also writing fully stylable macros in TeX isn't exactly easy...) Some document classes (KOMA, memoir) provide more flexibility. – Caramdir Jul 05 '12 at 04:35
  • @Caramdir, this does not make sense to me. Given that many publishers must want to accept LaTeX submissions and want their output to be typeset according to a distinctive house style rather than only according to the rules of a generic LaTeX preset, how do those publishers override/modify/etc LaTeX defaults in order to create their house styles? –  Jul 05 '12 at 10:30

2 Answers2

3

Let's tackle only the first example. LaTeX declares \emph with

\DeclareTextFontCommand{\emph}{\em}

so we need to look up what \em does:

\DeclareRobustCommand\em{%
  \@nomath\em
  \ifdim \fontdimen\@ne\font >\z@
    \upshape
  \else
    \itshape
  \fi}

The first instruction, \@nomath\em raises an error if the command is found in math mode, otherwise it does nothing. The conditional checks whether the current font is slanted (in this case \fontdimen1\font is positive); if so it issues \upshape, otherwise \itshape.

So, if you want \emph to choose "boldface italic" in an upright context and "upright boldface" in an italic context you have to say

\DeclareRobustCommand\em{%
  \@nomath\em
  \ifdim \fontdimen\@ne\font >\z@
    \upshape\bfseries
  \else
    \itshape\bfseries
  \fi}

Is this a general method? No.

I won't even think to the changes necessary to print verbatim material on a grey background: the fancyvrb package provides such a feature and its code is very complicated.

In LaTeX you can't simply hand calls to the browser like HTML does. The two models are completely different.

egreg
  • 1,121,712
  • I'm grateful for this, but although it addresses one of my examples, it doesn't really answer my question. How did you establish what LaTeX declares \emph (or any other command) to be? Why was this your first step? How did you look up what \em (or any other command) does? Where should the third code snippet you gave be put in order to apply to all instances of \emph in the present document? These are the sorts of things my question is getting at. –  Jul 04 '12 at 18:11
  • 3
    @sampablokuper To look things up: \show\emph (etc.) in your document or texdoc source2e. – Caramdir Jul 04 '12 at 18:30
0

As egreg says there is no general way -- each one needs to be considered separately. So for the \emph you could redefine the command in the preamble:

enter image description here

Note:

  • As egreg pointed out, this required the use of \LetLtxMacro macro from the letltxmacro package instead of \let since \emph is declared with \DeclareRobustCommand.
  • As Caramdir pointed out this may or may not be what is desired for nested use \emph{}. This redefinition yields an alternating bold italic, with bold upright, where as the original definition yields an alternating non-bold italic, with a non-bold upright as shown above.

Code:

\documentclass{article}
\usepackage{letltxmacro}

\LetLtxMacro\OldEmph\emph \renewcommand{\emph}[1]{\textbf{\OldEmph{#1}}}%

\begin{document} \OldEmph{orignal emph}\par \emph{modified emph}\par \emph{modified emph with a \emph{nested emph}} \end{document}

Peter Grill
  • 223,288
  • \emph is basically defined with \DeclareRobustCommand and it's quite dangerous to redefine it in this way (see the documentation of letltxmacro. – egreg Jul 04 '12 at 18:07
  • @egreg, but if letltxmacro were used instead of let then this approach would generally be OK? Or would there still be a danger - and if there is still a danger, then what would it be? –  Jul 04 '12 at 18:19
  • @egreg: Thanks, I was mistaken in thinking that \LetLtxMacro was only required when there were optional parameters. Have updated the solution. – Peter Grill Jul 04 '12 at 18:22
  • This of course might or might not do what you intend when nesting \emphs. – Caramdir Jul 04 '12 at 18:35