You need to avoid to read in the argument with the wrapper macro as this would fix the category codes of the argument and \lstinline
cannot do its magic anymore. So, here you go:
\newcommand\codeinline{\lstinline[style=inlinecode]}
This way you can also use the syntax with delimiters:
\codeinline+&$%+
This works since a macro that has no argument simply is replaced by its definition. So
\codeinline+&$%+
becomes
\lstinline[style=inlinecode]+&$%+
which works as intended.
Here is code demonstrating different approaches, based on your tests. The last case is probably what you want. My comments are below the code.
\documentclass{article}
\usepackage{listings}
\begin{document}
\lstset{%
basicstyle=\ttfamily\small,%
rangeprefix=;;MARK\ ,%
includerangemarker=false,%
texcl=false
}
$-$texcl, $-$comment
\begin{lstlisting}[linerange=startx-endx]
;; Some commentary before the marker
;;MARK startx
;;; High level $a_1$
a line to include ; with comment $y$
another code line to include $b_n$
;; a comment line to include $x^2$
;;MARK endx
;; Stuff after the range marker
\end{lstlisting}
$+$mathescape, $-$comment
\lstset{mathescape=true}
\begin{lstlisting}[linerange=startx-endx]
;; Some commentary before the marker
;;MARK startx
;;; High level $a_1$
a line to include ; with comment $y$
another code line to include $b_n$
;; a comment line to include $x^2$
;;MARK endx
;; Stuff after the range marker
\end{lstlisting}
$-$texcl, $+$comment
\lstset{texcl=false,mathescape=false,comment=[l];;}
\begin{lstlisting}[linerange=startx-endx]
;; Some commentary before the marker
;;MARK startx
;;; High level $a_1$
a line to include ; with comment $y$
another code line to include $b_n$
;; a comment line to include $x^2$
;;MARK endx
;; Stuff after the range marker
\end{lstlisting}
$+$texcl, $+$comment
\lstset{texcl=true,comment=[l];,morecomment=[l];;,morecomment=[l];;;}
\begin{lstlisting}[linerange=startx-endx]
;; Some commentary before the marker
;;MARK startx
;;; High level $a_1$
a line to include ; with comment $y$
another code line to include $b_n$
;; a comment line to include $x^2$
;;MARK endx
;; Stuff after the range marker
\end{lstlisting}
\end{document}
Firstly the markers code ranges should be defined to be strings that start at the beginning of the, so in your case they should include the ;;
marker. Changing this gets the correct lines of code included in your output in all cases.
Now note that the texcl
option does nothing if no syntax for comments has been defined. There is however the mathescape
option that can be used to turn expressions $...$
in to mathematics, but this will be throughout the code listing, as in the second example above.
Syntax wise there is a different behaviour for the comment declarations depending on whether the texcl
option is on or not. Without texcl
it is sufficient to declare ;
as the comment prefix, see example 3. With texcl
multiple ;
get swallowed, unless you add the appropriate combinations ;;
and ;;;
with the morecomment
option. I suspect that this is not the intended behaviour that the textcl
should affect this, but you will see the effect when trying examples.
Best Answer
At first an input such as
can seem more appealing than
but eventually it doesn't reveal such. For one thing:
\end{lstlisting}
is much more evident in the input than a single brace.Besides, there are technical reasons why the "macro with argument" is difficult to implement for
lstlisting
: this environment is pretty much likeverbatim
(but does more complicated things) and so it can't go inside the argument to another command, if you want that it treats correctly all the characters which are special to LaTeX (braces,#
,$
and%
, in particular).A good text editor can help, but also
listings
features: if you want to give particular options for typesetting chunks of code, you can define a new environment:and then
will be typeset applying the
<common options>
. Not very harder to type and good for marking your input and making it easy to find the chunks of code that use that common setting. You can also add "local options" by saying