# Set italic and bold inside of \lstlisting

The following effect is what I want:

And I wrote corresponding code as below

\begin{lstlisting}[mathescape]
\textbf{Document} ::= <Document [\textbf{\emph{documentName}}]$_0^1$  [\textbf{\emph{author}}]$_0^1$ [\textbf{\emph{date}}]$_0^1$>
[\textbf{Table}]$_1^n$  [\textbf{Action}]$_0^n$  [\textbf{Comment}]$_0^n$
</Document>
\end{lstlisting}


These code do worked, but It can't show italic and bold, the following is its result.

• maybe \lstset{ escapeinside={}} is useful. But I don't know the exact usage. Waite for your answer! – Huang Mao Jun 7 '15 at 7:04

You have already enabled mathescape. This can also be used for executing \textbf, e.g. $\textbf{Table}$.

I would use markup commands, then the code is easier to read and maintain. Also there are other options than a listings. The following example uses a tabbing environment:

\documentclass{article}
\usepackage{amstext}

\newcommand*{\EndTag}[1]{%
\textless /#1\textgreater
}
\newcommand*{\StartTagBegin}[1]{%
\textless #1%
}
\newcommand*{\StartTagEnd}{\textgreater}
\newcommand*{\Attribute}[1]{%
\textbf{\itshape#1}%
}
\newcommand*{\AttributeSpec}[3]{%
\Spec{\Attribute{#1}}{#2}{#3}%
}
\newcommand*{\Element}[1]{%
\textbf{#1}%
}
\newcommand*{\ElementSpec}[3]{%
\Spec{\Element{#1}}{#2}{#3}%
}
\newcommand*{\Spec}[3]{%
[#1]$_{\text{#2}}^{\text{#3}}$%
}
\newcommand*{\IsDefinedAs}

\begin{document}
\begin{tabbing}
\Element{Document} \IsDefinedAs\ \= \StartTagBegin{Document}
\AttributeSpec{documentName}{0}{1}
\AttributeSpec{author}{0}{1}
\AttributeSpec{date}{0}{1}\StartTagEnd
\\