7

Consecutively to this question how to avoid the transfer of the outside of a node inside and this answer : Nesting tikzpictures, style inheritance and save box, it comes to my knowledge that the same tikzpicture code has different outputs.

The following code looks like this with pdflatex and lualatex

enter image description here

But with xelatex :

enter image description here

It is an interesting alternative and it would be usefull to dispose of both possibilities depending on the choice of the author of the picture. The code could be reused for another application with another tex engine.

Question 1 : Is there a possibilty to benefit of the xelatex behavior in pdflatex and lualatex engines and vice-versa ?

Question 2 : What is the trick used in xelatex to protect from inheritance ?

\documentclass{standalone}
\usepackage{tikz,ifluatex}
\usetikzlibrary{automata, positioning}

\ifluatex
    \let\pdfpagewidth\pagewidth
    \let\pdfpageheight\pageheight
\fi

\newsavebox{\blankbox}
\savebox{\blankbox}{\hspace{0.1ex}\tikz[baseline=0.1em]{%
    \node [shape=rectangle, anchor=south, draw, inner sep=0pt, minimum width=1ex, minimum height=0.9em] (char) {}}%
  \hspace{0.1ex}}

\begin{document}
  \begin{tikzpicture}[auto, shorten >=1pt, >=latex]
    \node[state] (0) {\usebox{\blankbox}};
    \node[state, dotted, right=1ex of 0] (1) {\usebox{\blankbox}};
  \end{tikzpicture}
\end{document}
Tarass
  • 16,912

1 Answers1

10

This happens as the system layer of pgf deliberately resets dash style when inserting a box with the (x)dvipdfmx driver. If you look at the code there, you find:

\def\pgfsys@hbox#1{%
  \pgfsys@beginscope%
    \setbox#1=\hbox{\box#1}%
    \wd#1=0pt%
    \ht#1=0pt%
    \dp#1=0pt%
    \pgfsys@dvipdfmx@suspendcontent%
    \pgfsys@invoke{0 J [] 0 d}% reset line cap and dash
    \pgfsys@dvipdfmx@start@force@reset@color%
    \box#1%
    \pgfsys@dvipdfmx@stop@force@reset@color%
    \pgfsys@dvipdfmx@unsuspendcontent%
  \pgfsys@endscope%
}

whereas for pdfTeX you get

\def\pgfsys@hbox#1{%
  \pgfsys@beginscope%
    \setbox#1=\hbox{\box#1}%
    \wd#1=0pt%
    \ht#1=0pt%
    \dp#1=0pt%
    \box#1%
  \pgfsys@endscope%
}

One could I think argue for either behaviour as 'correct', though I'd favour the latter. You can happily add the 'reset' line to the pdfTeX version (the format is the same).

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • It very interesting. It is possible to swich to one behaviour to another on demand. If I need a specific behaviour (safe for the used tex engine) can one switch to it and then restore to a the usual (depending from the tex engine) beahvior. Is it safe to redef inside a group ? How to deal the with @ caracter that supposed to be used in the preamble. – Tarass Mar 17 '18 at 10:50
  • isn't the discrepancy a TikZ's bug? (independently of one's own preferred way) –  Mar 17 '18 at 10:58
  • @Tarass You can happily redefine within \makeatletter .. \makeatother – Joseph Wright Mar 17 '18 at 13:39
  • @jfbu I'd say so, yes: if anything, the dash and capping should be reset by \pgfpicture (wrapped up inside \begin{tikzpicture}), in the same way line width is. – Joseph Wright Mar 17 '18 at 13:50
  • @JosephWright I uderstand that. What does \def\pgfsys@hbox#1 define ? newsavebox, scope or something else. My point is define a new environment for drawing without enheritance safe - I mean without to perturb the usual beahvior of the used tex engine for classic environment. – Tarass Mar 17 '18 at 14:00
  • @Tarass \pgfsys@hbox is used to insert TeX boxes in a picture, and is used 'behind the scenes' in how \node works. You could if you wanted define an environment with a switch for turning resetting on-and-off, but as already commented this is really a bug: the behaviour should be defined one way or the other. – Joseph Wright Mar 17 '18 at 14:06
  • I think the wider 'what should happen' is likely one for a chat room or similar ... – Joseph Wright Mar 17 '18 at 14:08
  • @JosephWright I understand that it is a bug as all tex engines have to ouput same document. Dispite that, it might be interesting to overright the non buggy behavior. Let say the bug is in xelatex code, using lualatex I would appriciate to dispose of a box that prevent from inheritance. – Tarass Mar 17 '18 at 14:21
  • @Tarass “all tex engines have to ouput same document” That is absolutely not (resp. no longer) true. LuaTeX has a completely different approach to hyphenation than traditional TeX engines. But I agree that this behaviour is sort of unexpected. – Henri Menke Mar 17 '18 at 21:33