61

This question led to a new package:
embrac

Bringhurst, in Elements of Typographic Style, recommends to use upright parentheses in italic text (i.e., write 'a (simple) example' as opposed to 'a (simple) example'). I tend to agree, it looks nicer. But, how do I achieve this using LaTeX? One could of course write \emph{a \emph{(}simple\emph{)} example}, but that would get old real soon.

Two approaches I've thought of:

  • Make () (and possibly []) active characters, and ensure that they're always typeset upright, or
  • Do some wizardry with virtual fonts or somesuch.

I might be able to implement the first suggestion with my knowledge, but my gut tells me the second option is more elegant. Unfortunately, my knowledge of (virtual) fonts in LaTeX is even more limited than my knowledge of (in)active characters and catcodes and such. Any ideas on how to go about this?

Psychonaut
  • 3,142
Michel
  • 1,672
  • 11
  • 16
  • Making [] active will break optional arguments. Same for () and picture environments. – Martin Scharrer Mar 09 '11 at 13:46
  • 2
    @Martin Scharrer: making [( active but \let to themselves would not break \futurelet parsing. Then, they can be redefined to grab the corresponding closing brace inside \emph. – Bruno Le Floch Mar 09 '11 at 14:57
  • The issue that brought me to this page was the extra space that appears after the parenthesis and before the emphasised text when you write (\emph{Very low}): There is too much space between the ( and the V. The simplest and quickest solution I decided to use is just to take the space away with ! as ($!$\emph{Very low}). –  May 25 '12 at 13:27
  • Interestingly enough Bringhurst doesn't seem to follow his own advice: in chapter 1 "The Grand Design" of The Elements Of Typographic Style on the first page he cites a bill "[...] high heeled shoes [or] bolstered hips [...]" using sloped square brackets. Or is "[or]" part of the original text...? – cgnieder Jul 18 '12 at 16:04
  • Just a note: the Chicago Manual of Style recommends that the parentheses too are in italics, if the word they enclose is in italics. – Mårten Aug 28 '13 at 14:38

5 Answers5

34

A LaTeX3 solution. I chose to simply do what you said, replacing every ( by \textup{(}, every ) with \textup{)}, and similarly for brackets, prior to passing the result to the old version of \emph for typesetting.

At the end of the day, xparse allows us to easily define \emph to do what you asked for, and \emph* to do the old version of \emph.

\documentclass{article}
\usepackage{expl3,xparse}
\ExplSyntaxOn
\cs_new_eq:Nc \emph_old:n { emph~ } % Copying the old definition of `\emph`
\cs_new_protected:Npn \emph_braces:n #1 % Set up how braces should be typeset.
  { \mode_if_math:TF {#1} { \textup{#1} } }
\cs_new:Npn \emph_new:n #1 {
  \tl_set:Nn \l_emph_tl {#1}
  \tl_replace_all:Nnn \l_emph_tl {(}{\emph_braces:n{(}}
  \tl_replace_all:Nnn \l_emph_tl {)}{\emph_braces:n{)}}
  \tl_replace_all:Nnn \l_emph_tl {[}{\emph_braces:n{[}}
  \tl_replace_all:Nnn \l_emph_tl {]}{\emph_braces:n{]}}
  \exp_args:NV \emph_old:n \l_emph_tl
}
\RenewDocumentCommand {\emph} {sm} {
  \IfBooleanTF {#1} {\emph_old:n {#2}} {\emph_new:n {#2}}
}
\ExplSyntaxOff
\begin{document}
A \emph{(simple) example}, and \emph*{another one (with no correction)}.
Also some math \emph{\((x+y)^2\) and text (again)}.
\end{document}

EDIT: Alan Munn pointed out a mistake in a comment. Thanks.

EDIT2: An update to expl3 renamed \tl_replace_all_in:Nnn to \tl_replace_all:Nnn.

EDIT3: Barbara Beeton pointed out that I should be using \textup rather than \emph to set parentheses upright. She also mentioned that "it was suggested at one time to create a 'theorem font' in which the alphabet is italic and fences (parens, brackets, braces) are upright", but this font was never made. See comments below.

EDIT4: I had been sloppy when copying the definition of \emph, and this got revealed by the latest xparse update. LaTeX2e's \emph (like many other commands) uses \emph  to hold the real code of \emph (note the trailing space). The line below \ExplSyntaxOn was thus changed to \cs_new_eq:Nc ... { emph~ }: the "c" argument specifier turns emph~ into the appropriate command \emph .

EDIT5: I've added code to avoid changing braces in math mode. Or rather the command used to typeset braces is now math aware (this is much simpler than trying to detect math when doing the replacement).

  • 1
    This has the problem I allude to in my answer: if you embed it inside another \emph you will get italic parentheses within your upright text, which is probably not what you want! – Alan Munn Mar 09 '11 at 14:46
  • 1
    If you replace the \emph{(} with \textup{(}, it should work. – Michel Mar 09 '11 at 14:51
  • 1
    @Alan: thank you, I fixed it essentially as @Michel is saying. – Bruno Le Floch Mar 09 '11 at 14:54
  • 1
    This looks like what I meant indeed, it has the advantage of not needing a command to parenthesise something. I'll have to read up on LaTeX3 syntax, because I see a lot of unknown stuff here :D – Michel Mar 09 '11 at 15:05
  • @Bruno this is a really nice solution altogether. You should turn it into a fixemph package and submit it to CTAN. – Alan Munn Mar 09 '11 at 17:57
  • @Alan: it has a big flaw, though: \emph{Bla {(bla)} } would not fix those parenthesis. – Bruno Le Floch Mar 10 '11 at 08:54
  • @Bruno Is that fixable? (It's still useful as it, IMO) – Alan Munn Mar 10 '11 at 12:16
  • Next level: theorem styles seem to use \itshape rather than \emph, can you adapt your code to handle those? – Andrew Stacey Mar 14 '11 at 09:22
  • @Alan: thinking about it again, I think that it is fixable by detokenizing the argument of \emph and retokenizing it afterwards using \scantokens. I'll thikn about it some more. – Bruno Le Floch Mar 14 '11 at 15:04
  • @Andrew: I really don't see how to that without making braces active. Although, together with Will's environ package, something could be done. – Bruno Le Floch Mar 14 '11 at 17:17
  • @Bruno: Ah, well. I just saw the italicised braces in one of my theorems and thought, "Yuk". In the end, I made the theorem font upright anyway so don't worry about it. – Andrew Stacey Mar 14 '11 at 19:03
  • @BrunoLeFloch, unfortunately I get an Undefined control sequence. \emph_new:n ...emph_tl {#1}\tl_replace_all_in:Nnn. Any ideas what might go wrong? – domwass Dec 09 '11 at 16:37
  • @domwass: Thanks, \tl_replace_all_in:Nnn was deprecated some time ago, and renamed \tl_replace_all:Nnn. I fixed the code above. – Bruno Le Floch Dec 09 '11 at 21:00
  • @Bruno -- when you edited the answer to use \textup you only did it in the code, not in the first sentence, so someone who reads only the text will still get the impression that \emph should be used to make these fences vertical. – barbara beeton Dec 09 '11 at 22:27
  • it was suggested at one time to create a "theorem font" in which the alphabet is italic and fences (parens, brackets, braces) are upright. there's some disagreement whether numerals should be upright or italic. if they're math, clearly upright (but those should be coded $9$ anyhow), but if text, it's not so clear. how to handle xrefs? in ams document classes, \ref in an italic environment is set in italic, but \eqref is always set upright. so the situation is murky. in any event, no such font was ever created, so the question remains unanswered. – barbara beeton Dec 09 '11 at 22:50
  • @barbarabeeton thanks. I've updated and quoted your mention of the inexistent theorem font. – Bruno Le Floch Dec 10 '11 at 00:32
  • @BrunoLeFloch, Thanks, it works like a charm now! – domwass Dec 11 '11 at 20:01
  • @BrunoLeFloch, now I get another error: Runaway text? \emph code {\BooleanFalse }{(simple) example}{\emph_braces:n {(}simpl\ETC. ./klammern-aufrecht-emph.tex:21: TeX capacity exceeded, sorry [main memory size =3000000]. Has anything changed again? – domwass Feb 03 '12 at 13:22
  • @domwass I had been sloppy earlier when copying the old \emph to a new name. This was ok before because xparse used different function names internally, but the most recent changes are using \emph internally. See edit. – Bruno Le Floch Feb 03 '12 at 18:33
  • Thanks again! I have no clue about expl3, so I would be lost without your help! – domwass Feb 03 '12 at 19:13
  • @BrunoLeFloch I found this answer through a detour via “Die TeXnische Komödie” (2,2012). I'd very much like to make this (well - a little more extended :) ) into a package and was wondering if I could steal your basic idea/code (and publish it in a package under the LPPL)? – cgnieder Jun 28 '12 at 09:28
  • @cgnieder Of course you can. Thanks for asking. – Bruno Le Floch Jun 29 '12 at 05:42
  • This package seem cannot handle \emph{(math) nomath}. – Ma Ming Jun 06 '14 at 22:50
  • It does not work with italic text in theorem. – Ma Ming Jun 06 '14 at 23:05
  • @MaMing I've added code to handle math (if I understood your request correctly). It is much harder to handle theorems, as the contents of the theorem are never read as the argument of some macro. You could/should ask a separate question about that, with details of what your constraints are. – Bruno Le Floch Jun 07 '14 at 00:59
  • @cgnieder In case you have indeed made such a package, note my last edit to handle maths. – Bruno Le Floch Jun 07 '14 at 01:00
  • @BrunoLeFloch thanks. I actually added more or less the same to my package a few weeks ago... – cgnieder Jun 07 '14 at 08:22
  • Can this be applied to a colon as well? Using more of the same rules for : or \: or \c_colon_str or \ExplSyntaxOff:\ExplSyntaxOn still gives me italic. – gnucchi Mar 18 '18 at 16:33
  • 1
    @svenper: Try \tl_replace_all:Non \l_emph_tl { \token_to_str:N : }{...} with ... replaced as appropriate and perhaps you need to define the variant using \cs_generate_variant:Nn \tl_replace_all:Nnn { No } (put that outside other definitions, it only needs to be done once, not every time there is a colon). – Bruno Le Floch Mar 18 '18 at 20:31
27

I would agree not messing with catcodes (although doing so just within an \emph command itself probably wouldn't have too many issues.) But here's a relatively simple solution with a slightly more transparent semantics than Yiannis's solution:

\documentclass{article}

\newcommand{\bemph}[1]{{\upshape#1}} % define how emphasised brackets should look
\newcommand{\ep}[1]{\bemph{(}#1\bemph{)}} % parentheses
\newcommand{\eb}[1]{\bemph{[}#1\bemph{]}} % square brackets

\begin{document}

This is \emph{a \ep{simple} example}.
\emph{This is \emph{a \ep{simple} example}.} % also works embedded in another emph


\end{document}

The advantage of doing things this way is that if you ever need to change what \emph does, you have independent control over what emphasised brackets are. For example if you redefine \emph as underlining or \textbf you can adjust the brackets accordingly.

Alan Munn
  • 218,180
8

I would keep it simple and not mess up with catcodes and the like. I would define a small macro as follows (you can use a shorter name if you like).

\documentclass{article}
\usepackage{xspace}
\def\bracketemphasis#1#2{(\emph{#1})\xspace\emph{#2}\xspace}
\begin{document}
\bracketemphasis{simple}{example}.
\end{document}

The xspace would correctly handle spacing after punctuation and that is why I included it. \xspace should be used at the end of a macro designed to be used mainly in text. It adds a space unless the macro is followed by punctuation characters.

yannisl
  • 117,160
  • 1
    @Yiannis: Can you please expand on the spacing after punctuation? What could go wrong without the \xspace? – Hendrik Vogt Mar 09 '11 at 14:42
  • @Hendrik thanks, I added a short note as requested. – yannisl Mar 09 '11 at 14:57
  • @Yiannis: I'm confused about who of the two of us is confused now. Where and why would you get a space if you don't use \xspace? (I only know that \xspace is useful for macros that don't take arguments.) – Hendrik Vogt Mar 09 '11 at 15:00
  • @Hendrik Vogt I added it primarily to handle as for example This is an \bracketemphasis{example}{text}\bracketemphasis{test}{test}. I modified the explanation to make it more clear, or I hope so:) – yannisl Mar 09 '11 at 17:26
  • @Yiannis: I still don't get it. 1. You're still talking about punctuation there; 2. in your last comment, why don't you leave it to the user to put the space before the 2nd \bracketemphasis? (But OK, maybe it's good as a failsafe, so #2 is not important). – Hendrik Vogt Mar 09 '11 at 18:03
  • @Yiannis: I'm slowly getting it. But your sentence "\xspace should be used at the end of a macro designed to be used mainly in text" is still plain wrong. It should be used for macros without arguments. For macros with arguments, one usually doesn't use it. Please correct that! – Hendrik Vogt Mar 10 '11 at 16:50
  • @Yiannis: Sorry that I keep nagging, but this one sentence is still wrong. You can choose: Edit, explain, or I'll keep nagging forever :-) – Hendrik Vogt Mar 14 '11 at 15:28
  • @Yiannis, in case you wonder why I keep nagging at you: You are a high rep user, and people will put more trust in your answers being correct. – Hendrik Vogt Mar 14 '11 at 17:58
  • @Hendrik Vogt ... there is no problem with me, if you wish to edit the answer. From the manual abstract ... \xspace should be used at the end of a macro designed to be used mainly in text. It adds a space unless the macro is followed by certain punctuation characters (http://dante.ctan.org/get/macros/latex/required/tools/xspace.pdf). – yannisl Mar 15 '11 at 03:15
  • 2
    @Yiannis: Ah, it's from the manual, I see. Thanks for the explanation! Maybe the abstract of the manual should indeed say more clearly "at the end of a (parameterless) macro" (of course without the emphasis). That's my point: Your use of \xspace in a macro with parameters seems rather unusual, and this should be explicated in the answer. – Hendrik Vogt Mar 15 '11 at 07:33
5

This is a fairly different method than the previous one, so I'm posting it separately. The idea here is to make (, ), [, ] active in the whole document. Of course, that breaks everything, so we need to repair it by redefining the interface of every command that we use which takes optional arguments. I chose the syntax to be as close to xparse as possible. For instance,

\ChangeCommandInterface {\section} {t*d[]m} {s{#1}, o{#2}, m{#3}}%

will redefine \section to take three arguments: an optional star (t*), an optional argument delimited by active brackets (d[]), and a mandatory argument (m). Then the argument is reconstructed with the correct catcodes (we could also use that to swap arguments): in xparse, s means optional star, and here we put a star if and only if #1 is true (meaning that there was initially a star). Similarly, o{#2} will mean "put #2 as an optional argument, enclosed in (normal) brackets", and if there was no argument #2, then put no optional argument.

The example document, followed by the code.

% We put the body of the file here, and call it later using \input.
% Of course you can put the rest of this code in a style file
% and \usepackage{...} it in a normal document.
\begin{filecontents}{\jobname-body.tex}
  \documentclass{article}
  \usepackage{amsthm}
  \usepackage{amsmath}
  \newtheorem{theorem}{Theorem}
  \begin{document}

  \tableofcontents
  \section[Short title]{Long title}

  \begin{theorem}
    Test theorem (yes, I'm not inspired \textbf{(at all)}).
    \begin{align*}
      a\left[ \frac{ \sqrt[3]{c} }{ \sqrt[2]{d} } \right] \\
    \end{align*}
  \end{theorem}

  \end{document}
\end{filecontents}

\RequirePackage{xparse}
\ExplSyntaxOn

% We define functions to store the arguments into a token list
% for use later. Each type of argument has its own "put" function
% 
\tl_new:N \l_PWparse_args_tl

\cs_new:Npn \PWparse_put_cmd:N #1 {
  \tl_set:Nn \l_PWparse_args_tl {#1} }
\cs_generate_variant:Nn \PWparse_put_cmd:N {c}
\cs_new:Npn \PWparse_put_saved_cmd:N #1 {
  \PWparse_put_cmd:c {PWparse_saved_cmd_ \cs_to_str:N #1} }
\cs_new:Npn \PWparse_save_cmd:N #1 {
  \cs_new_eq:cN {PWparse_saved_cmd_ \cs_to_str:N #1} #1}



\cs_new:Npn \PWparse_put_arg:n #1 {
  \tl_put_right:Nn \l_PWparse_args_tl {#1}}

\cs_new:Npn \PWparse_put_targ:Nn #1 #2 {
  \IfBooleanT {#2} { \exp_args:No \PWparse_put_arg:n {\token_to_str:N #1} }
}
\cs_new:Npn \PWparse_put_sarg:n #1 { \PWparse_put_targ:Nn * {#1} }

\cs_new:Npn \PWparse_put_Darg:NNnn #1 #2 #3 #4 {
  \IfNoValueTF {#4} {
    \PWparse_put_Darg_aux:oon {\token_to_str:N #1} {\token_to_str:N #2} {#3} 
  }{
    \PWparse_put_Darg_aux:oon {\token_to_str:N #1} {\token_to_str:N #2} {#4}
  }
}
\cs_new:Npn \PWparse_put_Darg_aux:nnn #1 #2 #3 {
  \PWparse_put_arg:n {#1 #3 #2} }
\cs_generate_variant:Nn \PWparse_put_Darg_aux:nnn {oo}
\cs_new:Npn \PWparse_put_Oarg:nn #1 #2 {\PWparse_put_Darg:NNnn [ ] {#1} {#2} }

\cs_new:Npn \PWparse_put_darg:NNn #1 #2 #3 {
  \IfNoValueF {#3} { \PWparse_put_Darg:NNnn #1 #2 {} {#3} } }
\cs_new:Npn \PWparse_put_oarg:n #1 {\PWparse_put_darg:NNn [ ] {#1} }
\cs_new:Npn \PWparse_put_uarg:nn #1 #2 {
  \PWparse_put_arg:n { #2 #1 } }
\cs_new:Npn \PWparse_put_marg:n #1 {
  \PWparse_put_arg:n { {#1} }}
\cs_new_eq:NN \PWparse_put_larg:n \PWparse_put_arg:n
\cs_new:Npn \PWparse_put_garg:n #1 {
  \IfNoValueF {#1} {\PWparse_put_marg:n {#1}} }
\cs_new:Npn \PWparse_put_Garg:nn #1 #2 {
  \IfNoValueTF {#2} {
    \PWparse_put_marg:n {#1}
  } {
    \PWparse_put_marg:n {#2}
  }
}
\cs_new_eq:NN \PWparse_put_Darg:w \PWparse_put_Darg:NNnn
\cs_new_eq:NN \PWparse_put_darg:w \PWparse_put_darg:NNn
\cs_new_eq:NN \PWparse_put_garg:w \PWparse_put_garg:n
\cs_new_eq:NN \PWparse_put_larg:w \PWparse_put_larg:n
\cs_new_eq:NN \PWparse_put_marg:w \PWparse_put_marg:n
\cs_new_eq:NN \PWparse_put_oarg:w \PWparse_put_oarg:n
\cs_new_eq:NN \PWparse_put_Oarg:w \PWparse_put_Oarg:nn
\cs_new_eq:NN \PWparse_put_sarg:w \PWparse_put_sarg:n
\cs_new_eq:NN \PWparse_put_targ:w \PWparse_put_targ:Nn
\cs_new_eq:NN \PWparse_put_uarg:w \PWparse_put_uarg:nn
\cs_new:Npn \PWparse_put_one_arg:N #1 {\use:c{PWparse_put_ #1 arg:w}}


% To change the interface of a command, we save it (equivalent 
% of the primitive \let), and then treat arguments one at a time).
% 
\cs_new:Npn \ChangeCommandInterface #1 #2 #3 {
  \PWparse_save_cmd:N #1
  \DeclareDocumentCommand{#1}{#2}{%
    \PWparse_put_saved_cmd:N #1
    \clist_map_inline:nn {#3} {\PWparse_put_one_arg:N ####1}
    \l_PWparse_args_tl
  }
}
\ExplSyntaxOff

\begingroup
  \catcode`\[=13\catcode`\]=13\relax
  \catcode`\(=13\catcode`\)=13\relax
  \AtBeginDocument{%
    \catcode`\[=13\catcode`\]=13\relax
    \catcode`\(=13\catcode`\)=13\relax
    \def[{\ifmmode\string [\else\textup{\string [}\fi}%
    \def]{\ifmmode\string ]\else\textup{\string ]}\fi}%
    \def({\ifmmode\string (\else\textup{\string (}\fi}%
    \def){\ifmmode\string )\else\textup{\string )}\fi}%
    %
    % Complete the list below with your own commands.
    \ChangeCommandInterface {\section}    {t*d[]m} {s{#1}, o{#2}, m{#3}}%
    \ChangeCommandInterface {\subsection} {t*d[]m} {s{#1}, o{#2}, m{#3}}%
    \ChangeCommandInterface {\sqrt}       {d[]m}   {o{#1}, m{#2}}%
  }
\endgroup



\input\jobname-body.tex
David Carlisle
  • 757,742
1

My 2 cents: amslatex has \textup (equivalent to \rom), which is designed precisely for that (of course, you have to put it manually in all needed places).

Also, I am pretty sure that this should be easily doable in luatex.

mbork
  • 13,385
  • 2
    \textup is a standard LaTeX 2e command. amsart does define \upn as shorthand for \textup. You can use \rom for \textup in amsart only if you're in compatibility mode. – MSC Apr 09 '14 at 15:33