[Tex/LaTex] Set italic and bold inside of \lstlisting

boldcodeitalic

The following effect is what I want:
enter image description here

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.
enter image description here

Thanks for your solutions!

Best Answer

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
  \\
  \>\quad
    \ElementSpec{Table}{1}{n}
    \ElementSpec{Action}{0}{n}
    \ElementSpec{Comment}{0}{n}
  \\
  \> \EndTag{Document}
\end{tabbing}
\end{document}

Result