TransWikia.com

color{} inserts blank line after environment!

TeX - LaTeX Asked on March 4, 2021

I use begin{@empty}color{red} . . . end{@empty} to delimit colored text. But it inserts an extra blank line after end{@empty} in the following situation:

documentclass{article}
usepackage{xcolor}
begin{document}
begin{@empty}color{red}%
Paragraph starts abcd abcd abcd abcd abcd abcd abcd abcd
[
  a = 1
]%
end{@empty}%

New paragraph ijkl ijkl ijkl ijkl ijkl ijkl ijkl ijkl ijkl
end{document}

If you comment out color{red}, this blank line is gone! I wonder how to prevent this blank line while using color{red}.

By the way, it’s a bit annoying that you need % like end{@empty}% to prevent the empty line even without color{red}.

Ultimately I’d like an environment which has absolutely no impacts except for limiting the range of color{red}.

I use lualatex and pdflatex from the latest texlive distribution.

Edit: I failed to make it clear that my goal is to define an environment that colors the enclosed text and has not other impacts. In the process of trying to write such an environment, I encountered the above problem. My ultimate goal is to switch between colored and uncolored text quickly and systematically with a minimal markup. The monochrome option David mentions below would work except I still don’t know how to avoid the extra blank line. (Except I could remove the empty line after the math and use hspace*{parindent} to indicate the beginning of the paragraph.) The luacolor solutions is ideal except I don’t know how to make sure my publisher uses luatex. . . . (I mentioned pdflatex and lualatex only to imply the problem occurs on both compilers.)

3 Answers

documentclass{article}
usepackage{xcolor}
newenvironment{Color}[1][red]{color{#1}}{ignorespacesafterend}
begin{document}
begin{Color}
Paragraph starts: abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd
[
  a = 1
]
end{Color} 
New line---not new paragraph---starts: ijkl ijkl ijkl ijkl ijkl ijkl ijkl ijkl ijkl

begin{Color}[blue]
Paragraph starts: abcd abcd abcd abcd abcd abcd ab
end{Color} 
New line---not new paragraph---starts: ijkl ijkl ijkl ijkl ijkl ijkl ijkl ijkl ijkl
end{document}

enter image description here

Answered by user187802 on March 4, 2021

Groups in TeX are not no-op they have effects, so if you want to have no observable effect on spacing (which is actually surprisingly hard) you need to compensate for the group, not just do nothing.

You can see a similar effect even with a simple {} group.

a b

and

a  b

produce the same output as the multiple spaces are combined, however

a {} b

makes a double space, you need

a {}b

or (closer to the form you used)

a {}%
b

or (closer to the form I suggest below)

a {aftergroupignorespaces}
b

to get a single space between a and b.

LaTeX has a standard command to use ignorespaces after the group made by an environment, ignorespacesafterend so this produces the correct spacing without needing %

documentclass{article}
usepackage{xcolor}
newenvironment{zz}{}{ignorespacesafterend}
begin{document}
begin{zz}%color{red}%
Paragraph starts abcd abcd abcd abcd abcd abcd abcd abcd
[
  a = 1
]
end{zz}

New paragraph ijkl ijkl ijkl ijkl ijkl ijkl ijkl ijkl ijkl
end{document}

enter image description here

However if you add the colour

enter image description here

Note the extra line, really is an extra (white) line of the first paragraph, it is not vertical space, and would not be dropped at a page break.

The fact that it is hard to avoid colour commands affecting spacing is why the color package has the monochrome option that allows you to disable all colour commands rather than comment them out, this at least makes the spacing consistent between colour and black versions of the document.

The problem is that the color command inserts (again via aftergroup) a colour special to restore the colour, but the presence of this special (or pdf literal primitive) interferes with the end-of-display-math logic for deciding if there is a continuation of the paragraph after the display.

You could (here at least) disable the automatic colour restore and add it by hand in a safer place.

documentclass{article}
usepackage{xcolor}
newenvironment{zz}{}{ignorespacesafterend}
makeatletter
defrestorecolor{reset@color}
defnorestorecolorreset@color{}
makeatother
begin{document}
begin{zz}aftergroupnorestorecolorcolor{red}%
Paragraph starts abcd abcd abcd abcd abcd abcd abcd abcd
[
  a = 1
restorecolor
]
end{zz}

New paragraph ijkl ijkl ijkl ijkl ijkl ijkl ijkl ijkl ijkl
end{document}

enter image description here

Answered by David Carlisle on March 4, 2021

David has explained what happened. But as you mention lualatex: There you could avoid the problem by using luacolor, which uses attributes instead of specials and so doesn't disturb the math spacing:

documentclass{article}
usepackage{xcolor,luacolor}
begin{document}
begin{@empty}color{red}%
Paragraph starts abcd abcd abcd abcd abcd abcd abcd abcd
[
  a = 1
]%
end{@empty}%

New paragraph ijkl ijkl ijkl ijkl ijkl ijkl ijkl ijkl ijkl
end{document}

enter image description here

Answered by Ulrike Fischer on March 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP