Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm trying to change a custom environment from a lot of optional parameters to a ;-separated list (using this answer as a template and writing its main functionality as a latex3 macro).

The macro should put each of the tokens in the list on a separate line, formatted with a monospace font (it's source code). I previously used \lstinline from listings, but it seems to be incompatible with the latex3-variable. Using \texttt instead works.

I'd rather stay with \lstinline than with \texttt, because I need the postbreak feature it provides for marking breaks in long lines.

To reproduce the problem:

\documentclass{minimal}
\usepackage{listings,xparse}

\lstset{basicstyle=\ttfamily,breaklines=true,columns=fullflexible}

\NewDocumentCommand{\code}{m}{\textbf{\texttt{#1}}}  % works

% uncomment for error
%\NewDocumentCommand{\code}{m}{\lstinline{#1}}       % results in "Improper
                                                     % alphabetic constant."

\ExplSyntaxOn
\NewDocumentCommand{\methods}{m}{\methods_main:n {#1}}

\seq_new:N \l_methods_body_seq
\cs_new:Npn \methods_main:n #1
{
    \noindent
    \seq_set_split:Nnn \l_methods_body_seq {;} { #1 }
    \seq_pop_right:NN \l_methods_body_seq \l_tmpa_tl
    \seq_map_inline:Nn \l_methods_body_seq { \code{##1} \\ }
    \code{\tl_use:N \l_tmpa_tl} %<---- error occurs in this line
}
\ExplSyntaxOff

\begin{document}

\methods{monoSpacedLine1();int methodSignature();const int ANSWER 42}

\end{document}

The error occurs with every argument to the \methods macro I tried. I also already tried using \tl_to_str:N instead of \tl_use:N, or putting extra braces around it.

Is it possible to expand the variable in a way that \lstinline accepts it?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

You have to expand the token list before \code can see it; the cheapest method is with \exp_args:NV:

\documentclass{article}
\usepackage{listings,xparse}

\lstset{basicstyle=\ttfamily,breaklines=true,columns=fullflexible}
\NewDocumentCommand{\code}{m}{\lstinline{#1}}

\ExplSyntaxOn
\NewDocumentCommand{\methods}{m}{\methods_main:n {#1}}

\seq_new:N \l_methods_body_seq
\tl_new:N \l_methods_last_tl
\cs_new_protected:Npn \methods_main:n #1
 {
  \noindent
  \seq_set_split:Nnn \l_methods_body_seq {;} { #1 }
  \seq_pop_right:NN \l_methods_body_seq \l_methods_last_tl
  \seq_map_inline:Nn \l_methods_body_seq { \code{##1} \\ }
  \exp_args:NV \code \l_methods_last_tl
 }
\ExplSyntaxOff

\begin{document}

\methods{monoSpacedLine1();int methodSignature();const int ANSWER 42}

\end{document}

This will produce the same as

\code{<value of \l_methods_last_tl>}

which is what's desired.

Note that \methods_main:n is defined with \cs_new_protected:Npn because it does "unexpandable things" such as setting variables.

However, a preferred method would be to use an "inner" function also for \code:

\documentclass{article}
\usepackage{listings,xparse}

\lstset{basicstyle=\ttfamily,breaklines=true,columns=fullflexible}

\ExplSyntaxOn
\NewDocumentCommand{\methods}{m}{\methods_main:n {#1}}
\NewDocumentCommand{\code}{m}{\methods_code:n {#1}}

\cs_new_protected:Npn \methods_code:n #1
 { \lstinline{#1} }
\cs_generate_variant:Nn \methods_code:n { V }

\seq_new:N \l_methods_body_seq
\tl_new:N \l_methods_last_tl
\cs_new_protected:Npn \methods_main:n #1
 {
  \noindent
  \seq_set_split:Nnn \l_methods_body_seq {;} { #1 }
  \seq_pop_right:NN \l_methods_body_seq \l_methods_last_tl
  \seq_map_inline:Nn \l_methods_body_seq { \methods_code:n {##1} \\ }
  \methods_code:V \l_methods_last_tl
 }

\ExplSyntaxOff

\begin{document}

\methods{monoSpacedLine1();int methodSignature();const int ANSWER 42}

\end{document}
share|improve this answer
    
thanks a lot. I had tried to define such an "inner function" before, but defining \methods_code:V directly instead of using \cs_generate_variant. That produced the same error, can you explain why the \cs_generate_variant is mandatory in this case? –  crater2150 Mar 5 '13 at 12:25
1  
@crater2150 With \cs_new... you can define only macros with n and N type arguments; after that you can use \cs_generate_variant:Nn that will define a new macro that's the original with a suitable \exp_args:... prepended. –  egreg Mar 5 '13 at 13:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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