4

I would like to define a command using verbatim like in the commented line in the following code.

\documentclass{article}
    \usepackage{amsmath,amsfonts,amssymb}

%   \newcommand{\example}[1]{\verb!#1! : #1}

\begin{document}

\noindent \verb!$\textbf{A}^\intercal$! : $\textbf{A}^\intercal$
\\
\example{$\textbf{A}^\intercal$}$

\end{document}

How can I acheive this? Is it possible to give the argument without the dollars?

David Carlisle
  • 757,742
projetmbc
  • 13,315

1 Answers1

5

The question that @Marco refers to Verbatim text can't be shown correctly in pdf and the referenced Why doesn’t verbatim work within attempt to explain the difficulties with verbatim commands within macros.

But based on your code in the MWE \newcommand{\example}[1]{\verb!#1! : #1} it appears that you want to be able to show the LaTeX code and its output:

enter image description here

One way to do that would be to use the LTXexample environment from the showexpl package:

\documentclass{article}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage{xcolor}
\usepackage{showexpl}

\lstdefinestyle{demoLatexStyle}{ basicstyle=\small\ttfamily, numbers=none, frame=none, backgroundcolor=\color{yellow}, pos=r }

\begin{document} \begin{LTXexample}[style=demoLatexStyle] $\textbf{A}^\intercal$ \end{LTXexample} \end{document}

David Carlisle
  • 757,742
Peter Grill
  • 223,288