2

This is part of a larger problem, but I seem to be stuck at the trivial(ish) stage.

I want to centre a coffin containing a picture relative to the paper. I hoped to do this using the coffins poles, but I can't seem to make that work. I can do it if I know the width of the picture, so I could, if necessary, check the width and then use the result. However, I'm wondering if there is a simpler approach which would not require that.

Note that this example necessarily produces bad boxes, as must any satisfactory answer.

\begin{filecontents}{pic.tex}
\documentclass{standalone}
\begin{document}
\begin{tikzpicture}
  \shade [left color=Cerulean, right color=Cerulean, middle color=MidnightBlue] (0,0) rectangle (200mm,10mm);
\end{tikzpicture}
\end{document}
\end{filecontents}
\documentclass[a4paper,x11names,svgnames,dvipsnames]{article}
\usepackage{geometry,standalone,tikz,xcoffins,calc}
\begin{document}
\NewCoffin\TestCoffin
\SetHorizontalCoffin\TestCoffin{\input{pic}}

This works:\par
\noindent
\TypesetCoffin\TestCoffin[l,vc](-100mm+.5\textwidth,0pt)
\bigskip

This doesn't:\par
{\centering
  \TypesetCoffin\TestCoffin[hc,vc]\par}
\bigskip

Can I centre the coffin \emph{without} knowing the width of the picture?

\end{document}

I expected that, if I was at the centre of the page, using hc would centre the coffin. Since the margins are equal by default in a one-sided article, I expected this to set the picture centrally on the paper, too.

That is, I expected to get the same result from both code samples above. But not only do I not get that, I don't actually have any idea what I get instead. \TypesetCoffin doesn't ignore the \centering and it does not ignore the hc, either, but I'm not at all clear what exactly it does with them. (Or if it is \TypesetCoffin's doing at all.)

unexpected (to me) result

What do I not understand?

cfr
  • 198,882
  • You cannot make any box go out of the left margin unless you remove it's width (\llap) or insert negative glue before it, cf. this example. – Henri Menke Jun 12 '17 at 00:29
  • @HenriMenke But what does LaTeX do with the poles in this case? Note that it is going into the left-margin. It is too much into the left margin, in fact. So, whatever is true of boxes, clearly coffins can go into the left margin. In fact, both of the coffins above go into the left margin. I assume the first inserts negative glue. I don't know what the second does or why. If it is meant to use hc and it is \centering, why does it end up like that? Your point would explain a shift to the right, but not the left. – cfr Jun 12 '17 at 00:49
  • @HenriMenke I could understand if it refused to put more than 100mm of the coffin to the left of the centre of the text block. Then the second block would start at the left margin and go off the page on the right. But that isn't what is happening. It is instead putting more than 10mm of the coffin to the left of the centre of the text block. What I don't understand is why. – cfr Jun 12 '17 at 00:56
  • In the second case, to get alignment with the hv pole, the width of the coffin is reduced to half the width with the left part sticking out. So the right half of the coffin is centred. – Henri Menke Jun 12 '17 at 01:00
  • @HenriMenke But why? – cfr Jun 12 '17 at 01:03
  • That's how the coffin placement mechanism works. If you want something like "place it at the centre of the page sticking into both margins", use TikZ. See this document for more illustration on my last comment https://www.overleaf.com/read/tsmxpypsqrmv – Henri Menke Jun 12 '17 at 01:04
  • @HenriMenke Never mind, I see why. I wasn't asking about the coffin placement. I was just not thinking about what \centering must do. I can't use TikZ as I'd be nesting tikzpictures. – cfr Jun 12 '17 at 01:13
  • @HenriMenke Will you answer? With https://tex.stackexchange.com/questions/374464/can-i-centre-my-oversized-coffin-of-unknown-width#comment926286_374464 ? – cfr Jun 12 '17 at 01:14

3 Answers3

4

You can draw it centered inside an \linewidth-wide \hbox via \hss:

\begin{filecontents}{pic.tex}
\documentclass{standalone}
\begin{document}
\begin{tikzpicture}
  \shade [left color=Cerulean, right color=Cerulean, middle color=MidnightBlue] (0,0) rectangle (200mm,10mm);
\end{tikzpicture}
\end{document}
\end{filecontents}
\documentclass[a4paper,x11names,svgnames,dvipsnames]{article}
\usepackage{geometry,standalone,tikz,xcoffins,calc}
\begin{document}
\NewCoffin\TestCoffin
\SetHorizontalCoffin\TestCoffin{\input{pic}}

Can I centre the coffin \emph{without} knowing the width of the picture?

\noindent
\hbox to\linewidth{\hss\TypesetCoffin\TestCoffin[l,vc]\hss}

\end{document}

enter image description here

It doesn't produce any warnings about overfull hboxes. However, it doesn't take left and right margin differences into account. This could be accommodated if needed.

TH.
  • 62,639
  • (+1) But please don't remove the latex3 tag from the question. Indeed, I really want a coffin-based explanation and answer here or, failing that, something in expl3. The latex3 tag covers xcoffins in any case. – cfr Jun 12 '17 at 00:43
  • This is exactly what I proposed in the comment. – Henri Menke Jun 12 '17 at 00:45
  • @HenriMenke, sorry, I don't see where you suggested that. The Overleaf link shows a rule extending into the right margin which was not the issue here. – TH. Jun 12 '17 at 00:58
  • @TH. »You cannot make any box go out of the left margin unless you remove it's width« Removing the width is what \hss does. – Henri Menke Jun 12 '17 at 01:05
  • @HenriMenke, Sorry, I'm still not following you. How does that comment imply that the solution is to change [hc,vc] to [l,vc] and put it in a box with \hss on either side? More importantly, I'm confused by what exactly you want me to do? Should I delete the answer? – TH. Jun 12 '17 at 01:14
  • No! Don't delete your answer. I merely wanted to point out the connection my between comment and your answer. – Henri Menke Jun 12 '17 at 01:18
3

The placement of the coffin using poles, in particular the hc pole, works in such a way that the width of the box is reduced such that the pole is at the left edge of the box (as detailed in the TeXbook, the reference point of a box is always at the left on the baseline of the enclosed material).

In this particular case (hc pole) this leads to the coffin being reduced to half its size with the left half sticking out. The \centering instructs TeX to centre the remaining right half. It becomes more clear once some markers are placed on the picture. As we expect, the marker at 150mm (the half of the right half) is now at the centre of the page.

\begin{filecontents}{pic.tex}
\documentclass{standalone}
\begin{document}
\begin{tikzpicture}
  \shade [left color=Cerulean, right color=Cerulean, middle color=MidnightBlue] (0,0) rectangle (200mm,10mm);
  \foreach \len in { 0, 50, ..., 200 }
    \draw[red,thick] (\len mm,10mm) -- (\len mm,0) node[below,overlay] {\len mm};
\end{tikzpicture}
\end{document}
\end{filecontents}
\documentclass[a4paper,x11names,svgnames,dvipsnames]{article}
\usepackage{geometry,standalone,tikz,xcoffins,calc,showframe}
\begin{document}
\NewCoffin\TestCoffin
\SetHorizontalCoffin\TestCoffin{\input{pic}}

This works:\par
\noindent
\TypesetCoffin\TestCoffin[l,vc](-100mm+.5\textwidth,0pt)
\bigskip

This doesn't:\par
{\centering
  \TypesetCoffin\TestCoffin[hc,vc]\par}
\bigskip

Can I centre the coffin \emph{without} knowing the width of the picture?

\end{document}

enter image description here

Henri Menke
  • 109,596
  • 1
    Thank you! I corrected the pole name. (There's no hv pole unless you make a custom one.) – cfr Jun 12 '17 at 01:29
1

Here's a coffin-based solution based on the answer which I hoped Henri Menke would write, and which Henri Menke has now written,. It is intended as a supplement to that answer.

This includes the non-working example, with the hc,vc coffin handle marked, as well as a working example, with the l,vc and hc,vc handles marked, which does not depend on knowing the width of the coffin or calculating it before hand. It just gets calculated as part of the coffin typesetting.

\begin{filecontents}{pic.tex}
\documentclass{standalone}
\begin{document}
\begin{tikzpicture}
  \shade [left color=Cerulean, right color=Cerulean, middle color=MidnightBlue] (0,0) rectangle (200mm,10mm);
\end{tikzpicture}
\end{document}
\end{filecontents}
\documentclass[a4paper,x11names,svgnames,dvipsnames]{article}
\usepackage{geometry,standalone,tikz,xcoffins,calc}
\begin{document}
\NewCoffin\TestCoffin
\SetHorizontalCoffin\TestCoffin{\input{pic}}

\MarkCoffinHandle\TestCoffin[hc,vc]{magenta}
\MarkCoffinHandle\TestCoffin[l,vc]{magenta}

Can I centre the coffin \emph{without} knowing the width of the picture?

This works:\par
\noindent
\TypesetCoffin\TestCoffin[l,vc](-.5\CoffinWidth\TestCoffin+.5\textwidth,0pt)
\bigskip

This doesn't:\par
{\centering
  \TypesetCoffin\TestCoffin[hc,vc]\par}


\end{document}

coffin-based solution & demo handles

cfr
  • 198,882
  • Trying to come to grips with coffins (the human condition? ;) ) This fails with `./Coffin_tikz_001.tex:26: Illegal unit of measure (pt inserted). . l.26 ...5\CoffinWidth\TestCoffin+.5\textwidth,0pt)

    ? ` yet this appears to be similar to other answers. What am I missing?

    – sgmoye Nov 07 '18 at 13:40
  • The problem would seem to be, at least in this case, fractional factors to \CoffinWidth\TestCoffin. Integer factors are OK. – sgmoye Nov 07 '18 at 13:49
  • Apparently there was a change in the definition of \CoffinWidth or the underlying l3 macros which breaks this answer. Take a look at https://tex.stackexchange.com/questions/458823/xcoffins-and-fractional-factors-to-coffinwidth-fail/458836#458836 for a follow up question with an answer explaining the issue. – Skillmon Nov 07 '18 at 18:37