I'm trying to generate a section title programatically from some constituent strings. The simplified MWE just concatenates them (the live system will do more complex processing) but I get a leading zero and intermediate characters ˙" (x3F 22). Input file is UTF8 encoded but the MWE strings should only be ASCII.


\documentclass[a4paper, 11pt, twoside, onecolumn, dvipsnames, final]{memoir}%




\newcommand{\Bsection[3]}{\directlua{Bsection(#1,#2,#3)}} % This defines the tex macro


\chapter{Chapter the first}

\Bsection{"Section pre-text"}{"T"}{"Section post-text"}

\section{\Bsection{"String pre-text"}{"T"}{"String post-text"}}


The relevant function in the TexMacros.lua file is :

function Bsection(s, sb, sc)
    local t=""
    t = t..s..sb..sc
    t = string.gsub(t, "[\192-\255][\128-\191]*", "")
    return t


Chapter 1
Chapter the first
0Section pre-textT"Section post-text"
1.10String pre-textT"String post-text"
  • 2
    never use [utf8]{inputenc} with luatex, you also should not need luatexbase other than for compatibility with very early lualatex realeases. Jul 4, 2018 at 20:06

1 Answer 1


You have the syntax for \newcommand wrong, you want

\newcommand{\Bsection}[3]{\directlua{Bsection(#1,#2,#3)}} % This defines the tex macro


\newcommand{\Bsection[3]}{\directlua{Bsection(#1,#2,#3)}} % This defines the tex macro
  • 1
    By the way, that wrong \newcommand is processed as if it were \newcommand\Bsection[3][0]{\directlua{#1,#2,#3}}, so that's where the 0 is coming from.
    – egreg
    Jul 4, 2018 at 20:12
  • Obvious when it's pointed out :-) Much appreciated David.
    – DLyons
    Jul 4, 2018 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.