12

My goal is to reduce typing and at the same time keep the document as readable as possible.

Issue at hand:

I want to change the font for one word in a particular sentence. For example, I want to switch from the document's default font to monospace font AND at the same time preserve the default font for the remaining of the sentence.

I came up with the following solution:

\documentclass[letterpaper]{article}

\usepackage{lmodern}
\newcommand{\lightfont}{\ttfamily \fontseries{l}\selectfont light font}
\newcommand{\mediumfont}{\ttfamily \fontseries{m}\selectfont medium font}
\newcommand{\boldfont}{\ttfamily \fontseries{b}\selectfont bold font}

\begin{document}
Is there an easier, simpler and  more elegant way to switch font
from {\lightfont}  to {\mediumfont} and to {\boldfont} 
and, at the same time, preserve the default text font.

\end{document}

What I do not like is:

  • curly braces {} around the command, for example {\mediumfont}
  • the \newcommand definition is a little bit too complex for such a simple task
  • the \texttt and its siblings like \textbf could not produce \lightfont font
  • Since you want to restrict your font changes, you must use a {...}. I suggest to use macros with an argument here, however – user31729 Sep 4 '16 at 23:42
  • 1
    I don't understand what you mean by you last two bullets... Can you elaborate? – Werner Sep 4 '16 at 23:44
  • Regarding second bullet: Please, see the solution, proposed by @DavidCarlisle. It is short, readable and simple. Regarding the third bullet: I could not figure out how to use \textXX to create/generate \lightfont – Igor Sep 5 '16 at 6:58
14

The standard \textxxx commands do almost all that you want, you just need a command for light series:

enter image description here

\documentclass[letterpaper]{article}

\DeclareRobustCommand\lseries{\fontseries{l}\selectfont}



\usepackage{lmodern}


\begin{document}
Is there an easier, simpler and  more elegant way to switch font
from \texttt{\lseries light}  to \texttt{medium} and to \texttt{\bfseries bold} 
and, at the same time, preserve the default text font.

or with local shortcuts

\newcommand\ttl[1]{\texttt{\lseries#1}}
\newcommand\ttm[1]{\texttt{\mdseries#1}}
\newcommand\ttb[1]{\texttt{\bfseries#1}}

Is there an easier, simpler and  more elegant way to switch font
from \ttl{light}  to \ttm{medium} and to \ttb{bold} 
and, at the same time, preserve the default text font.

\end{document}
  • (+1) Though you will not be surprised to learn that this is not how I would do it, given that the OP is using Latin Modern ;). – cfr Sep 5 '16 at 1:07
14

Disclaimer: I wrote the package described below.

Caveat emptor ...

cfr-lm provides access to all Latin Modern fonts supplied in type1 formats by TeX Live. This includes access to the 4 sets of figures, as well as various other features, including weights and widths not supported by LaTeX's New Font Selection Scheme.

For example

\documentclass[letterpaper]{article}

\usepackage[sf={lining,proportional},tt={monowidth,lining,tabular},rm={lining,proportional}]{cfr-lm}

\begin{document}
Is there an easier, simpler and  more elegant way to switch font from \texttt{\textlg\textcd{{light condensed}}}  to \texttt{\textlg{light}}  to \texttt{medium} and to \texttt{\textbf{bold}} and, at the same time, preserve the default text font.

Or \texttt{\lgweight\cdwidth light condensed} to \texttt{\lgweight light} to \textt{medium} to \texttt{\bfseries bold}.

\end{document}

Latin Modern with whistles

A variable width typewriter font is also available and may be more appropriate for this use than a mono-width one. (It is hard to know from the example.)

For code, mono-spacing is useful, but for typewriter text, it is less so.

\documentclass[letterpaper]{article}

\usepackage[sf={lining,proportional},tt={variable,lining,tabular},rm={lining,proportional}]{cfr-lm}

\begin{document}
Is there an easier, simpler and  more elegant way to switch font from \texttt{\textlg{light}}  to \texttt{medium} and to \texttt{\textbf{bold}} and, at the same time, preserve the default text font.

Or \texttt{\lgweight light} to \textt{medium} to \texttt{\bfseries bold}.

\end{document}

typewriter rather than mono-spaced

If you need to typeset code, you will need mono-width. However, cfr-lm provides commands which allow you to switch to a non-default font temporarily (i.e. using \textXX{} or {\fontswith } or until further notice (i.e. \fontswitch). This provides access to mono-width typewriter with tabular, lining figures in a document where \ttfamily is set to variable typewriting with proportional, hanging figures, for example.

You can also access some additional fonts e.g. the specialist 'quotation' font, the Latin Modern versions of the Dunhill fonts, slashed zeros, italic small-caps, upright italics and more. See the documentation for details.

Here's a more complete sampler.

Latin Modern sampler

\documentclass[a3paper]{article}
\usepackage{geometry,microtype}
\geometry{hscale=.9,vscale=.9}
\usepackage[sf={lining,proportional},tt={variable,lining,tabular},rm={lining,proportional}]{cfr-lm}
\newcommand*\nos{0123456789\zeroslash}
\newcommand*\tester[1][]{\noindent{{\normalfont\texttm{cfr-lm}} #1provides easier, simpler and more elegant ways to switch between Latin Modern fonts. \textpo{\nos{}} \textpl{\nos} \textto{\nos} \texttl{\nos}\par}}
\begin{document}
\section*{Roman}
\tester
\tester[\slshape]
\tester[\itshape]
\tester[\uishape]
\tester[\scshape]
\tester[\sishape]
\tester[\bfseries]
\tester[\bfseries\slshape]
\tester[\bfseries\itshape]
\tester[\sbweight]
\tester[\sbweight\slshape]

\section*{Sans}
\sffamily
\tester
\tester[\slshape]
\tester[\bfseries]
\tester[\bfseries\slshape]
\tester[\fontseries{sbc}\selectfont]
\tester[\fontseries{sbc}\selectfont\slshape]
\normalfont

\section*{Monowidth Typewriter}
\tmstyle
\tester
\tester[\slshape]
\tester[\itshape]
\tester[\scshape]
\tester[\sishape]
\tester[\bfseries]
\tester[\bfseries\slshape]
\tester[\lgweight]
\tester[\lgweight\slshape]
\tester[\lgweight\cdwidth]
\tester[\lgweight\cdwidth\slshape]
\normalfont

\section*{Variable Typewriter}
\ttfamily
\tester
\tester[\slshape]
\tester[\bfseries]
\tester[\bfseries\slshape]
\tester[\lgweight]
\tester[\lgweight\slshape]
\normalfont

\section*{Dunhill}% the documentation erroneously claims a bold version is available - it doesn't exist
\tistyle
\tester
\tester[\slshape]
\normalfont

\section*{Quotation Sans}
\qtstyle
\tester
\tester[\slshape]
\tester[\bfseries]
\tester[\bfseries\slshape]
\normalfont

\end{document}
4

If you don't like placing additional {...}, then you can place the font change inside a group within the \newcommand. However, you are then almost certainly bound to use xspace to automatically insert a space after a command, and that comes with its own caveats:

enter image description here

\documentclass{article}

\usepackage{lmodern,xspace}
\newcommand{\lightfont}{{\ttfamily \fontseries{l}\selectfont light font}\xspace}
\newcommand{\mediumfont}{{\ttfamily \mdseries medium font}\xspace}% \mdseries ~ \fontseries\mddefault\selectfont
\newcommand{\boldfont}{{\ttfamily \bfseries bold font}\xspace}% \bfseries ~ \fontseries\bfdefault\selectfont

\begin{document}
Is there an easier, simpler and  more elegant way to switch font
from \lightfont to \mediumfont and to \boldfont 
and, at the same time, preserve the default text font.

\end{document}
  • @cfr: Correct. Strange there was no error or warning with a b series... – Werner Sep 5 '16 at 2:48
  • bx is a silent substitution for b in the .fd files so there is no warning. This is very common. – cfr Sep 5 '16 at 2:53
  • Actually, I'm wrong. lm now defines b as a silent substitution for bx. At least for this font. – cfr Sep 5 '16 at 3:01

Your Answer

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

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