3

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.

MWE:

\documentclass{article}

\usepackage{luacode}
\usepackage{xspace}

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

\begin{document}
  \directlua{\luastring{\foo}}
\end{document}

Error:

This is LuaHBTeX, Version 1.13.0 (TeX Live 2021/Arch Linux) 
 restricted system commands enabled.
(./main.tex
LaTeX2e <2020-10-01> patch level 4
 L3 programming layer <2021-02-18>
(/usr/share/texmf-dist/tex/latex/base/article.cls
Document Class: article 2020/04/10 v1.4m Standard LaTeX document class
(/usr/share/texmf-dist/tex/latex/base/size10.clo))
(/usr/share/texmf-dist/tex/lualatex/luacode/luacode.sty
(/usr/share/texmf-dist/tex/generic/iftex/ifluatex.sty
(/usr/share/texmf-dist/tex/generic/iftex/iftex.sty))
(/usr/share/texmf-dist/tex/luatex/luatexbase/luatexbase.sty
(/usr/share/texmf-dist/tex/luatex/ctablestack/ctablestack.sty)))
(/usr/share/texmf-dist/tex/latex/tools/xspace.sty)
(/usr/share/texmf-dist/tex/latex/l3backend/l3backend-luatex.def)
(./build/main.aux) (/usr/share/texmf-dist/tex/latex/base/ts1cmr.fd))
! Incomplete \iffalse; all text was ignored after line 10.
<inserted text> 
\fi 
<*> main.tex
6
  • 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

5

\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.

\documentclass{article}

\usepackage{luacode}
\usepackage{xspace}

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

\begin{document}

\typeout{}

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

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

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

\end{document}

Produces

Expanded foo\xspace 1
Expanded foo 2
Expanded foo3
4

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

test.lua

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

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

main.tex

\documentclass{article}

\usepackage{luacode}
\usepackage{xspace}
\directlua{require "test"}

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

\newcommand{\Foo}[1]{{\let\xspace\space\directlua{Foo(\luastring{#1})}}}

\begin{document}
  \Foo{\foo}
  \Foo{\foo bar}
  \Foo{\foo. bar}
\end{document}

Result:

"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
  • 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 .