2

I draw a yellow square inside a grey square, exactly in the middle. Unexpectedly, the yellow square is not aligned horizontally but instead moved to the right:

enter image description here

Why? If I understand correctly, the yellow square should end at x=37, and the grey at x=38.

\documentclass{article}
\usepackage{xcolor}
\begin{document}
\setlength{\unitlength}{1mm}
\hrule\noindent

\begin{picture}(40,40)
\linethickness{36mm}
\color{gray}
\put(2,20){\line(1,0){36}}

\linethickness{34mm}
\color{yellow}
\put(3,20){\line(1,0){34}}
\end{picture}

\hrule
\end{document}
David Carlisle
  • 757,742
olpa
  • 1,116

1 Answers1

4

The picture mode commands make some attempt to ignore white space between the instructions but \color defeats that attempt so you are seeing the word space after \color (and other places). Note that TeX is designed as a text typesetting language promarily and white space is not syntactic decoration as in most programming languages, it is (usually) an instruction to add stretchable inter-word space at that point.

It is best to avoid typesetting anything in picture mode that is not inside \put so

\color{yellow}
\put(3,20){\line(1,0){34}}

not

\put(3,20){\color{yellow}\line(1,0){34}}

Although \color is in fact safe outside \put as long as you avoid adding space around it

\color{yellow}%

The definition of \linethickness has been changed (2020 release) to avoid this extra space.

https://github.com/latex3/latex2e/issues/274

David Carlisle
  • 757,742