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.

13
  • 3
    These are really very different questions. There is no procedure that can work in all cases.
    – egreg
    Jul 4, 2012 at 17:08
  • 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 4, 2012 at 17:42
  • 1
    OK, wrong example. But still there is no similarity between LaTeX and HTML, other than both use a kind of markup.
    – egreg
    Jul 4, 2012 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.
    – user15911
    Jul 4, 2012 at 17:50
  • 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 4, 2012 at 18:38

2 Answers 2

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.

2
  • 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.
    – user15911
    Jul 4, 2012 at 18:11
  • 3
    @sampablokuper To look things up: \show\emph (etc.) in your document or texdoc source2e.
    – Caramdir
    Jul 4, 2012 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}
4
  • \emph is basically defined with \DeclareRobustCommand and it's quite dangerous to redefine it in this way (see the documentation of letltxmacro.
    – egreg
    Jul 4, 2012 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?
    – user15911
    Jul 4, 2012 at 18:19
  • @egreg: Thanks, I was mistaken in thinking that \LetLtxMacro was only required when there were optional parameters. Have updated the solution. Jul 4, 2012 at 18:22
  • This of course might or might not do what you intend when nesting \emphs.
    – Caramdir
    Jul 4, 2012 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy