6

I am experiencing an odd issue with \textit and \emph. I have a list from plaintext and want to format it. Here is a minimum example demonstrating my issue:

\documentclass{article}

\begin{document}

\textit{ (a)

(b) }

\end{document}

Results in:

an image which does not perfectly match the source text. The item (a) is repeated and (b) is not

Why? What is going on here?

cgnieder
  • 66,645
Tom
  • 183
  • 1
    remove the space between (a) and (b) in your code and it compiles normaly. – Roland Mar 16 '21 at 07:27
  • 2
    Cool effect. But note the error message(s) you get: The argument of \textit must not contain an empty line or paragraph break. If you want to format more text, use {\itshape (a) \par (b)}. But LaTeX has better ways to produce lists (enumerate). – moewe Mar 16 '21 at 07:30
  • @Roland - Your advice may make the immediate source of the error go away, but it does so at the cost of destroying the OP's intended formatting of the lines. – Mico Mar 16 '21 at 07:34
  • Welcome to TeX.SE. – Mico Mar 16 '21 at 07:36
  • 1
    @Mico where does he say what it should look like in the end? He asked: What is going on here? So for me the answer was the extra blank space is what is going on. – Roland Mar 16 '21 at 07:41
  • @Roland - I'd go by the OP's sentence "I have a list from plaintext and want to format it." To me, that sounds very much like the line break is supposed to be preserved. – Mico Mar 16 '21 at 07:44
  • @Mico Yes, but I also had in mind that maybe he want to use it in mathmode. So far I know \itshape des not wok in mathmode, which would cause him further problems. – Roland Mar 16 '21 at 07:57
  • Just for the sake of completeness \textit allows line breaks with commands such as double backslash or \newline. So I think you could do it with \textit as well. – Roland Mar 16 '21 at 08:09
  • 2
    It should be noted though that \\ and \newline are not the same as an empty line or \par. It is a good rule of thumb that one should not use \\ or \newline in the normal text body of a document: Paragraphs should be ended with an empty line (or \par if that is more convenient or appropriate) and within paragraphs there should be no forced line breaks with \\. (Of course \\ has its use in align or table environments and might be useful occasionally to avoid a bad line break. @Roland) – moewe Mar 16 '21 at 16:47

1 Answers1

14

The command \textit does not permit paragraph breaks in its argument, while all-blank lines cause paragraph breaks. You must be getting the following error message:

Runaway argument?
{ (a) 
! Paragraph ended before \text@command was complete.
<to be read again> 
                   \par 
l.9 }

Any lessons to be learnt? I'd say there are 2. First, don't ignore error messages. Second, to get your code to compile and perform the expected formatting, change the code chunk to

{\itshape
  (a)

(b) }

Mico
  • 506,678
  • 2
    Thank you. This solves my problem. Do you know why it is like this though? It is unintuitive behaviour. For example in markup or html when you tag something as italic, it does not matter what kind of whitespace is included. Why does \textit treat whitespace differently than other characters? – Tom Mar 16 '21 at 20:25
  • 1
    @Tom that is how the macro is defined. There are two ways to specify, say italics, for short texts \textit, for longer we have the switch \itshape. Same thing in html, its kinda ad practice to mark huge amounts of text inside a <em></em>, what is where we have css. – daleif Mar 16 '21 at 21:47
  • @Tom - Sorry for not being clear enough. What's at issue is not whitespace per se, but the fact that TeX and LaTeX convert one or more all-blank input lines into a \par token, which generates a paragraph break. It so happens -- I'm afraid I can't answer the why part of your question knowledgeably -- that a particular choice was made in the design of the LaTeX commands \textit, textbf, \textsc etc: they are all not set up to tolerate paragraph breaks in their arguments. Clearly, this was a conscious design choice, not an oversight, as it applies consistently across these commands. – Mico Mar 16 '21 at 21:48
  • 1
    @Tom: Think of how in html, you have inline elements versus block elements, which are meant to be used differently. Similarly, in LaTeX, some commands (like \emph) are designed to be used just within paragraphs, while others (usually environments) are meant to be used for longer sections of text. – Peter LeFanu Lumsdaine Mar 16 '21 at 22:24
  • 2
    @Tom (La)TeX macros can be defined as taking 'long' or 'short' arguments. A long argument may contain paragraph end (\par or an empty line, which is converted to \par by TeX), but short arguments may not contain paragraph ends. Most explanations for that behaviour I have seen are about error detection: Many macros should only apply to a short stretch of text and not to whole paragraphs, so when the macro encounters a paragraph end, the user probably forgot a } to close the argument. ... – moewe Mar 17 '21 at 07:44
  • 2
    ... Especially in the old days, when documents took long to compile, it was a good idea to have a rogue macro argument stop absorbing text at that point and throw an error, so that a user can stop the process or at least syntax checking has a chance of resuming afterwards. See for example the explanations in https://tex.stackexchange.com/q/1050/35864 and https://tex.stackexchange.com/q/39450/35864. ... – moewe Mar 17 '21 at 07:48
  • 1
    ... \textit and \emph are defined as taking short arguments, so they will throw an error at the end of a paragraph. Presumably the idea is that \textit should be used to highlight short passages of text within a single paragraph and that you should be using the switch \itshape (possibly wrapped up in an environment definition) to typeset long passages of text spanning several paragraphs in italics. – moewe Mar 17 '21 at 07:49
  • @moewe - Many thanks for these additional explanations. They certainly square with my (admittedly hazy) recollection as to why the \text<..> commands are set up the way they are, i.e., that they're designed not to accept \par tokens in their arguments. I was reluctant to write that up as I wasn't sure whether my recollections were suffiiciently on target. – Mico Mar 17 '21 at 09:22