I am using lua code to automate recording todos that come up during meetings I take minutes of. For this I use \luastringN from the luacode package currently. However, I would like to have all the 'text' macros expanded in my arguments, since this would make it easier to export these tasks to other programs without cryptic macros. I found that the macros containing \xspace cause issues when replacing \luastringN with \luastring.




\newcommand{\foo}{Expanded foo\xspace}



This is LuaHBTeX, Version 1.13.0 (TeX Live 2021/Arch Linux) 
 restricted system commands enabled.
LaTeX2e <2020-10-01> patch level 4
 L3 programming layer <2021-02-18>
Document Class: article 2020/04/10 v1.4m Standard LaTeX document class
(./build/main.aux) (/usr/share/texmf-dist/tex/latex/base/ts1cmr.fd))
! Incomplete \iffalse; all text was ignored after line 10.
<inserted text> 
<*> main.tex
  • 1
    xspace is not expandable Nov 22, 2021 at 9:47
  • what do you want the effective Lua string to be given the input that you show? "Expanded foo\\xspace" or "Expanded foo " or "Expanded foo" any of those three would be possible by locally defining \xspace to have an appropriate definition. Nov 22, 2021 at 9:52
  • The result should depend on where the macro is used, so \luastring{\foo} I would like to expand to "Expanded foo", but \luastring{\foo bar} I would like to expand to `"Expanded foo bar". Could you point me to how to locally define xspace to something like that?
    – D Hoekstra
    Nov 22, 2021 at 9:59
  • You can't really really do that, you can hold the \xspace in the string as in my first code below. To get the effect you want I would use the second definition so it expands to space then use a Lua gsub to replace any double spaces by a single space. Nov 22, 2021 at 10:03
  • Ah okay. Thank you, I will use the idea you proposed then!
    – D Hoekstra
    Nov 22, 2021 at 10:04

2 Answers 2


\xspace is not expandable, so can't be directly used in \directlua (or \write or \edef or similar commands.

You can locally give it a definition that is safe in those contexts, depending on what result you want.



\newcommand{\foo}{Expanded foo\xspace}



{\let\xspace\relax\directlua{print(\luastring{\foo} .. 1)}}

{\let\xspace\space\directlua{print(\luastring{\foo} .. 2)}}

{\let\xspace\empty\directlua{print(\luastring{\foo} .. 3)}}



Expanded foo\xspace 1
Expanded foo 2
Expanded foo3

Based on the comments and the answer of @David Carlisle, I have written the following solution:


function xspace_trim(s)
    local res = string.gsub(s, " ([%p ])", "%1")
    local res = string.gsub(res, "^%s*(.-)%s*$", "%1")
    return res

function Foo(s)
  print("\"" .. s .. "\" --> \""..xspace_trim(s).."\"")



\directlua{require "test"}

\newcommand{\foo}{Expanded foo\xspace}


  \Foo{\foo bar}
  \Foo{\foo. bar}


"Expanded foo " --> "Expanded foo"
"Expanded foo bar" --> "Expanded foo bar"
"Expanded foo . bar" --> "Expanded foo. bar"

The function xspace_trim removes any double whitespace in the string as well as spaces before punctuation. It also removes any leading and trailing whitespace. The \xspace is locally defined as just a space. The lua code then takes care of any added spaces that were unwanted.

  • 1
    actually you should probably accept this as it's more useful to you and to future readers to have a positive answer rather than just "you can't do what you wanted" in mine, I can manage without the 15 points:-) Nov 22, 2021 at 14:30

You must log in to answer this question.

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