16
  • This question is a follow-up to this question ("pgfplots: Strange Bump in \tanh Function").

  • Stefan Pinnow, who knows a thing or two about pgfplots, provided an answer that distinguished between \addplot{tanh(\x)}; and \addplot{tanh(x)};(x vs \x as the argument).

  • In a comment, Stefan stated "My thought also was that tanh(\x) is the same as tanh(x) but it seems that Lua "doesn't like" "commands", i.e. \<something>. But to my experience I never used \x in \addplot calls. Since this is easier to read and easier to type [...]".

     % use TeX as calculation engine
     \addplot {tanh(\x)};
     % use Lua as calculation engine (when compiled with LuaLaTeX of course)
     \addplot+ [very thick] {tanh(x)};
    
  • The current pgfplots manual (Revision 1.18.1 (2021/05/15)) states in Chapter 4.3.3 (Computing Coordinates with Mathematical Expressions): "In short: it is the same whether you write \x or just x inside of math expressions.".

enter image description here

  • Question: In what circumstances does it matter whether I use x or \x in an addplot argument?
  • My understanding of this (which can be very wrong) is that with \x you are just passing the number literal such as 3.4567 to the function. TeX's macro expansion will do the job for you. But with x it undergoes some heavy csname-tricks because how in the world does PGF going to recognize a variable/function? – Symbol 1 Aug 24 '21 at 02:46
  • 4
    There was a period of time where TikZ learners use \draw plot ...; and it's impossible not to mess up sin(x) vs sin(\x) vs \sin(x) vs \sin(\x). Perhaps pgfplots's author is just tired of this and try to make the interface as friendly as possible. I myself always use sin(\x) because I like to see it as a \foreach\x in{...} thing. – Symbol 1 Aug 24 '21 at 02:48
  • But now comes Lua and it powerful math engine. What do you want to do? You want to send Lua a clean string sin(x) instead of any variant that contains escape character \. In that regard, sin(x) makes more sense. But of course, these are just pure guesses. I never work with lualatex because fontspec always fail =( – Symbol 1 Aug 24 '21 at 03:00
  • @Symbol1 Thanks for your comments! I was surprised that even Stefan had to guess :). – Dr. Manuel Kuehner Aug 24 '21 at 03:11
  • 1
    There may be other cases I don't know of (so not a complete answer), but at least one case is when Lua back end is used (note in chapter 6.3.1 [Reducing typesetting time → Lua]: "math expressions which involve macro definitions are unavailable in lua backend. Best practice: prefer declare function over macro constants" – The example after that shows the issue exactly. – user202729 Dec 28 '21 at 02:56
  • 1
    Here is an example where x and \x don't behave the same: https://tex.stackexchange.com/questions/663296/strange-syntax-in-pgfplot I think this may be a bug or at least a strange behavior... – Hérisson Didier Nov 09 '22 at 19:56
  • @HérissonDidier I opened a bounty in your honor :). – Dr. Manuel Kuehner Nov 10 '22 at 01:04
  • And the answers to my question ended by: if \x and x don't behave the same, there is a bug. – Hérisson Didier Nov 14 '22 at 00:08

1 Answers1

5

As far as I understand, this only matters when you want to use the lua backend. Section 6.3.1 (Reducing Typesetting Time -- LUA) of the pgfplots manual explains:

Math expressions which involve macro definitions are unavailable in lua backend. Best practice: prefer declare function over macro constants

This does not only apply to variables/functions you define yourself, but to those defined by pgfplots, too. \x is a macro which "disables" the lua backend for that \addplot and the numerically inferior TeX routines are used. x on the other hand is defined using (an equivalent of) declare function, so the lua backend can handle it.

If you don't use LuaTeX (or set lua backend to false), the TeX backend is always used and \x and x behave identically.


An MWE to illustrate the difference (compile using LuaLaTeX):

% !TeX program = lualatex
\documentclass{article}

\usepackage{pgfplots} \pgfplotsset{compat=1.18}

%\pgfplotsset{lua backend=false}

\begin{document}

\begin{tikzpicture} \begin{axis} \addplot[domain=6:8] {tanh(\x) - 1};
\end{axis} \end{tikzpicture}

\begin{tikzpicture} \begin{axis} \addplot[domain=6:8] {tanh(x) - 1};
\end{axis} \end{tikzpicture}

\end{document}

MWE output

Numerical inacurracies of the TeX backend lead to unexpected results in the first plot (using \x) that are not present in the second one (using x) which is calculated by the lua backend. Compiling with pdfLaTeX instead of LuaLaTeX or disabling the lua backend (by uncommenting the appropriate line) will lead to the second plot becoming identical to the first.

schtandard
  • 14,892