Print small TeX code verbatim and render it

For math mode this should work.

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\showcase}{v}
 {
  \texttt{#1} & \tl_rescan:nn { } { $#1$ }
 }
\ExplSyntaxOff

\begin{document}
\begin{tabular}{cc|cc}
\showcase{x^{y}}      & \showcase{\hat{x}, \bar{x}} \\
\showcase{x_{y}}      & \showcase{f\colon X \to Y} \\
\showcase{x'}         & \showcase{\sqrt{x+2}} \\
\showcase{x''_{2}}    & \showcase{2 < x \leq 4} \\
\showcase{A^{1}_{2}}  & \showcase{\frac{a+b}{c+d}} \\
\showcase{3\pi/4}     & \showcase{\int_{0}^{1} x^{2} \,dx} \\
\showcase{x\in\Omega} & \showcase{A \cup B \subseteq C \cap D}
\end{tabular}
\end{document}

enter image description here

Should you want to swap the columns, so the result appears before the code, just change the definition of \showcase inverting the cells:

\NewDocumentCommand{\showcase}{v}
 {
  \tl_rescan:nn { } { $#1$ } &  \texttt{#1}
 }

enter image description here


I created \detokenizeplus which detokenizes but removes spaces before left braces and underscores, which were the problems for your examples. Additionally, I remove the space between consecutive macro names (but can only detect the first macro if there is space before its name). The place where that should apply is in between \in\Omega. However, because \in was immediately preceded byx and not a space, my logic can't catch that case. So for this MWE, I made the argument x \in\Omega to test that I could trap consecutive macros.

The logic here is that \getargsC breaks up the input into space separated arguments (\argi, \argii, \argiii, etc.) which can be exploited to determine whether to add back a space or not when regurgitating the arguments sequentially.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{stringstrings}
\usepackage{readarray}
\newcommand\showcase[1]{{\ttfamily\detokenizeplus{#1}} & $#1$}
\def\firstchar#1#2|{#1}
% catcode=12 BACKSLASH
\edef\tbs{\detokenize{\X}}
\edef\tbs{\expandafter\firstchar\tbs|}
% catcode=12 LEFT BRACE
\edef\tlb{\detokenize{{}}}
\edef\tlb{\expandafter\firstchar\tlb|}
% catcode=12 UNDERSCORE
\edef\tus{\detokenize{_}}
\newcounter{index}
\newcommand\detokenizeplus[1]{%
  \def\temparg{\detokenize{#1}}%
  \getargsC{\temparg}%
  \setcounter{index}{0}%
  \def\prevmacro{F}%
  \whiledo{\value{index} < \narg}{%
    \stepcounter{index}%
%   DETERMINE IF A MACRO; REMOVE SPACE BETWEEN CONSECUTIVE MACROS
    \isnextbyte[q]{\tbs}{\csname arg\roman{index}\endcsname}%
    \if T\theresult%
      \if T\prevmacro\unskip\else\fi%
      \def\prevmacro{T}%
    \else%
      \def\prevmacro{F}%
   \fi%
%   REMOVE SPACES BEFORE LEFT BRACE
    \isnextbyte[q]{\tlb}{\csname arg\roman{index}\endcsname}%
    \if T\theresult\unskip\else\fi%
%   REMOVE SPACES BEFORE UNDERSCORES
    \isnextbyte[q]{\tus}{\csname arg\roman{index}\endcsname}%
    \if T\theresult\unskip\else\fi%
    \csname arg\roman{index}\endcsname~%
  }%
}
\begin{document}

\begin{tabular}{cc|cc}
\showcase{x^{y}}      & \showcase{\hat{x}, \bar{x}} \\
\showcase{x_{y}}      & \showcase{f\colon X \to Y} \\
\showcase{x'}         & \showcase{\sqrt{x+2}} \\
\showcase{x''_{2}}    & \showcase{2 < x \leq 4} \\
\showcase{A^{1}_{2}}  & \showcase{\frac{a+b}{c+d}} \\
\showcase{3\pi/4}     & \showcase{\int_{0}^{1} x^{2} \,dx} \\
\showcase{x \in\Omega} & \showcase{A \cup B \subseteq C \cap D}
\end{tabular}

\end{document}

enter image description here