3

I want to include some braces in my string variable:

\documentclass{article}
\usepackage{expl3}

\begin{document}
    \ExplSyntaxOn
    \str_new:N \l_example_var
    \int_new:N \l_example_int

    \str_set:Nn \l_example_var {\{}
    \str_put_right:NV \l_example_var {\l_example_int}
    \str_put_right:Nn \l_example_var {\}~some~text}

    \str_show:N \l_example_var
    \ExplSyntaxOff
\end{document}

Unfortunately this results in the braces being preceded by pesky backslashes:

> \l_example_var=\{0\} some text.
<recently read> }

l.13 ^^I\str_show:N \l_example_var

How would I be able to get rid of the backslashes in front of the braces? E.g. \l_example_var={0} some text.

5
  • \c_left_brace_str and \c_right_brace_str instead of \{ and \}, but are you sure you need str instead of a token list, i.e. \tl_set:N...?
    – user31729
    Dec 27, 2018 at 13:07
  • @ChristianHupfer Thanks for the help. Using a token list would indeed seem more fitting. However doing \tl_set:NV \l_example_tl { {\l_example_int} } results in the surrounding braces disappearing.
    – tambre
    Dec 27, 2018 at 13:57
  • \tl_set:Nx \l_example_tl { { \int_to_arabic:n { \l_example_int } } }
    – egreg
    Dec 27, 2018 at 14:07
  • @tambre: Your use-case wasn't clear, I could have done a \tl... like solution as well...
    – user31729
    Dec 27, 2018 at 17:16
  • @ChristianHupfer Sorry about that.
    – tambre
    Dec 27, 2018 at 18:20

2 Answers 2

4

I'm not sure what's the benefit of a string such as {42} and probably you need a token list instead. However,

\str_set:Nx \l_example_str { \c_left_brace_str \int_use:N \l_example_int \c_right_brace_str }

will do. For a token list it's similar

\tl_set:Nx \l_example_tl { { \int_use:N \l_example_int } }
5

Apparently, the { and } 'strings' shall appear literally in the string, so one way to add them is to use \c_left_brace_str and \c_right_brace_str and the V modifier for the \str_set:Nn macro

There are other literal string constants such as \c_hash_str or \c_dollar_str, which are constant token lists with catcode 12 actually. See page 75 in the current interface3 manual (as of 2018/12/27).

\documentclass{article}
\usepackage{expl3}

\begin{document}
    \ExplSyntaxOn
    \str_new:N \l_example_var
    \int_new:N \l_example_int

    \str_set:NV \l_example_var \c_left_brace_str
    \str_put_right:NV \l_example_var \l_example_int
    \str_put_right:NV \l_example_var \c_right_brace_str
    \str_put_right:Nn \l_example_var {~some~text}


    \str_show:N \l_example_var
    \ExplSyntaxOff
\end{document}

The output is

This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2018)

(preloaded format=pdflatex) \write18 enabled. entering extended mode

(./stringwith.tex LaTeX2e <2018-12-01>

(/usr/local/texlive/2018/texmf-dist/tex/latex/base/article.cls

Document Class: article 2018/09/03 v1.4i Standard LaTeX document class

(/usr/local/texlive/2018/texmf-dist/tex/latex/base/size10.clo))

(/usr/local/texlive/2018/texmf-dist/tex/latex/l3kernel/expl3.sty

(/usr/local/texlive/2018/texmf-dist/tex/latex/l3kernel/expl3-code.tex)

(/usr/local/texlive/2018/texmf-dist/tex/latex/l3kernel/l3pdfmode.def))

(./stringwith.aux)

\l_example_var={0} some text. }

l.16 \str_show:N \l_example_var

? x No pages of output. Transcript written on stringwith.log.

1
  • 1
    There should be no brace around a V argument. Not that braces make a difference, it's more on the conceptual side.
    – egreg
    Dec 27, 2018 at 14:00

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.