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?
8
  • 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, 2021 at 2: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, 2021 at 2: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, 2021 at 3:00
  • 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, 2021 at 2:56
  • 1
    Here is an example where x and \x don't behave the same: tex.stackexchange.com/questions/663296/… I think this may be a bug or at least a strange behavior... Nov 9, 2022 at 19:56

1 Answer 1

5
+100

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.

4
  • Thanks for your reply. So does your answer apply if I use pdflatex? Nov 11, 2022 at 8:49
  • @Dr.ManuelKuehner It does: If you use pdflatex the lua backend will never be active, so \x and x should always behave identically.
    – schtandard
    Nov 11, 2022 at 10:00
  • @Dr.ManuelKuehner I added an MWE you can play around with to see when lua backend is used and when it isn't.
    – schtandard
    Nov 11, 2022 at 10:11
  • Cool, thanks a lot! Nov 11, 2022 at 16:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .