0

My figure is somehow centered, but not exactly centered. Why?

A more general question would be: If I have a TikZ picture with already-computed coordinates, that I can't easily re-compute, how can I reduce and translate the picture so the result fits in the available width and is horizontally centered?

I found some similar problems on this site, with different suggestions ("Overfull \hbox" warning, ...), but I still don't understand.

Remarks:

  • I'm compiling with LuaTeX.
  • I don't have an "Overfull \hbox" warning (but if I use a bigger scalebox factor, I have the warning)
  • Even if I give TikZ some very-translated-to-the-right coordinates, it displays me the same triangle
  • I used many-digits decimal, because I thought there might be some rounding errors, but that doesn't seem to make any difference
  • My example is contrived, but is just a simplification of a bigger picture where I had the same problem
\documentclass[article]{jlreq}
\usepackage[a5paper,paperheight=1.00000cm,paperwidth=1.00000cm,top=0.00000cm, bottom=0.00000cm, left=0.00000cm, right=0.00000cm, twoside=false]{geometry}
\usepackage{tikz}
\begin{document}
\begin{figure}[h!]
    \centering
    \scalebox{0.455400}{
        \begin{tikzpicture}
        \filldraw [fill=red, draw=none] (12.00000, 1.50000cm) -- (11.00000cm, 1.00000cm) -- (13.00000cm, 1.00000cm);
        \end{tikzpicture}
    }
\end{figure}
\end{document}

Not centered triangle

Bernard
  • 271,350
Michaël
  • 124
  • Here is a similar question: https://tex.stackexchange.com/questions/294306/horizontal-shift-when-using-scalebox-and-input-within-center-environment – Jason Young Oct 17 '22 at 08:24

1 Answers1

3

I believe the issue is one purely of extra stray spaces introduced at line ends. Adding % to those line ends resolves it in the article class (I could not compile in the OP's document class).

\documentclass{article}
\usepackage[a5paper,paperheight=1.00000cm,paperwidth=1.00000cm,top=0.00000cm, bottom=0.00000cm, left=0.00000cm, right=0.00000cm, twoside=false]{geometry}
\usepackage{tikz}
\begin{document}
\begin{figure}[h!]
    \centering
    \scalebox{0.455400}{%
        \begin{tikzpicture}
        \filldraw [fill=red, draw=none] (12.00000, 1.50000cm) -- (11.00000cm, 1.00000cm) -- (13.00000cm, 1.00000cm);
        \end{tikzpicture}%
    }%
\end{figure}
\end{document}

enter image description here

  • Great! That's it! But I don't understand why the line ends are relevant (relevantly bad), whereas the spaces at the beginning of the lines are not. It's very confusing to me. Is there some known reference of why some spaces are relevant and some are not? Some tutorial? – Michaël May 22 '22 at 16:36
  • 1
    This is the relevant question, @Michaël: https://tex.stackexchange.com/questions/7453/what-is-the-use-of-percent-signs-at-the-end-of-lines-why-is-my-macro-creat. Line ends get converted into spaces in the normal mode of TeX token digestion. Spaces at the beginning of a line are (generally) ignored. – Steven B. Segletes May 22 '22 at 16:39
  • Great reference. And is there some way to display these invisible extra spaces in the final picture, for debugging purposes? (like in a word processor, when you can display spaces as little dots). – Michaël May 22 '22 at 17:06
  • Someone already asked a similar question, but it didn't get any answer: https://tex.stackexchange.com/questions/309049/paragraph-formatting-marks-similar-to-ms-word-option – Michaël May 22 '22 at 18:27
  • 1
    It's tricky, @Michaël. One can type {\catcode\\ =12 x y x y}which will make the spaces visible withT1encoding. However, it makes *all* spaces visible, even the ones that TeX would otherwise silently digest. For example, one explicit space or 5 explicit spaces are digested as a single space by TeX. Instead of trying to make things visible, I follow rules of thumb: leading spaces are ignored, spaces (including line ends) after control sequences are invisibly digested, line ends after anything except control sequences will produce a space, unless the line is terminated with a%`. – Steven B. Segletes May 22 '22 at 18:33
  • 1
    @Michaël This question is related, https://tex.stackexchange.com/questions/250551/is-it-possible-to-redefine-the-white-space-command, but as I already said, it will make all spaces visible, even those contiguous multiple spaces that would otherwise be digested as a single space by TeX. Heiko's answer there is as close as I've seen come. – Steven B. Segletes May 22 '22 at 18:46