lualatex-tools / luaoptions

Option handling for LuaLaTeX packages
MIT License
2 stars 0 forks source link

Treatment of spaces (around commas) in options #1

Open uliska opened 5 years ago

uliska commented 5 years ago

Moved from https://github.com/jperon/lyluatex/issues/256:

I'm not completely sure about this and I haven't come up with a MWE yet. But I have the impression that option parsing swallows spaces (at least around commas) in options.

I noticed that something like

{
  ['list-separator'] = { ', '}
}

in an option definition doesn't work, and I competely understand and accept that the comma has to be protected like so:

{
  ['list-separator'] = { '{, }'}
}

in order not to be mistaken as a value separator by the parser. However, the parser also seems to remove the space, and a simple ',' is what ends up being used.

{
  ['list-last-separator'] = { ' and '}
}

also doesn't work properly, so I end up with a list rendered as one,two,threeandfour.

Is there a way how spaces can be protected similarly to commas (which I would then document), or a way we can preserve these kinds of spaces?

(P.S.: it's not an option to have the client insert the spaces manually because there is no universal rule about it, maybe one will for example want one.two.three.four for the rendering of above list.)

uliska commented 5 years ago

I've now created a MWE to reproduce this issue:

comma.tex:

\documentclass{article}
\usepackage{luaoptions}
\usepackage{optionsetup}
\begin{document}
\triple{Text}
\end{document}

optionsetup.sty:

\NeedsTeXFormat{LaTeX2e}%
\ProvidesPackage{optionsetup}

\directlua{
  local _opts = lua_options
  _opts.register('commas', {
    ['comma'] = {'{, }', _opts.is_str },
    ['pipe'] = {'{ | }', _opts.is_str }
    })
  separators = require('separators.lua')
}

\newcommand*{\triple}[1]{\directlua{separators.format([[#1]])}}

separators.lua:

local separators = {}

function separators.format(text)
    local options = lua_options.client('commas')
    local comma, pipe = options.comma, options.pipe
    tex.print(text .. comma .. text .. pipe .. text)
end

return separators

This produces Text,Text|Text instead of Text, Text | Text.

uliska commented 5 years ago

OK, fortunately I found the issue myself after looking at https://tex.stackexchange.com/questions/468660/: There's nothing we can do on the luaoptions side but the user can work around the matter by protecting options with spaces by two braces:

  _opts.register('commas', {
    ['comma'] = {'{{, }}', _opts.is_str },
    ['pipe'] = {'{{ | }}', _opts.is_str }
    })

This seems to be a condition of core TeX processing and xkeyval's version of it, so we can only deal with it by documenting the issue. But that seems robust, so it's OK.