create macro to verbatim output of command [closed]

The code

\newcommand{\dosomething}[1]{#1 #1}
\expandafter\verb\expandafter+\dosomething{blub}+


produces the verbatim-output blub blub.

Now I want to create a macro \myverb for the second line, i.e., I want that

\myverb{blub}


or

\myverb+blub+


creates the verbatim-output blub blub.

How can I achieve this (in TeX or LaTeX)?

A follow up question is, how to achieve it with a verbatim-environment instead of \verb? Here a concrete example:

\begin{verbatim}
\begin{evaluatecode}
a = 1+1
b = 2^a
print a,b
\end{evaluatecode}
\end{verbatim}


But I want that the environment evaluatecode is expanded first (in my case, the calculations are sent to some software, an output is returned; evaluatecode already exists), and then, the result is displayed in verbatim. I.e., I want that the code

\begin{verbatimevaluate}
a = 1+1
b = 2^a
print a,b
\end{verbatimevaluate}


does the following: first the input of verbatimevaluate is processed by the evaluatecode-environment and its outcome stands in an verbatim-environment.

-

closed as unclear what you're asking by egreg, Svend Tveskæg, Martin Schröder, Guido, ChrisSFeb 18 at 0:14

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

Do you really need \verb for printing control sequence names and other special characters, or do you just need \texttt? I believe that the real application is different, isn't it? –  egreg Feb 17 at 13:11
\texttt does not work, I really need \verb. (The real application comes from using the sagetex-package, which is used for interaction with the computer algebra system Sage.) –  Daniel Krenn Feb 17 at 13:40
It still isn't very clear, I'm afraid. What do you mean by “doubling a verbatim environment”? –  egreg Feb 17 at 14:09
i wonder about the statement “\texttt does not work, I really need \verb” ... given that we have no example of what you are aiming for, we have to guess what you're doing that forces out \texttt –  wasteofspace Feb 17 at 16:06
@DanielKrenn No, sorry, it's not clear; what you need is interaction between LaTeX and some software, which depends on the software; loading the output produced by the external software as a verbatim listing is easy. –  egreg Feb 18 at 8:57

Note: you may upvote this answer ;-).

\documentclass{article}
\makeatletter
\def\myverb
{%
\relax\leavevmode\null
\bgroup
\verbatim@font
\let\do\do@noligs  \verbatim@nolig@list
\let\do\@makeother \dospecials
\@vobeyspaces
\@myverb
}%
\def\@myverb #1{\catcode#1\active
\lccode\~#1%
\lowercase{\long\def~##1~}{\myverbaction{##1}\egroup}%
\lowercase{~}}%
\makeatother

% customize as desired:
\newcommand{\myverbaction}[1]{#1 #1}%

\begin{document}\thispagestyle{empty}
\myverb+blub+

\myverb+\if\end&^_~%$\par\ifcase+ \myverb|some +-$^%_

multiline

input|
\end{document}


For better rendering of multiline things, something like the following (else one may follow more closely the LaTeX own verbatim).

\def\myverb
{%
\relax\leavevmode\null
\bgroup
\verbatim@font
\let\do\do@noligs  \verbatim@nolig@list
\let\do\@makeother \dospecials
\@vobeyspaces
% in case of multi-line input, something like this:
\obeylines\parskip\baselineskip \parindent\z@
\@myverb
}%


This used a \noindent which was not there in the first image (everything was indented in the first image, but the indentation was trimmed when converting to
a png).

\noindent\myverb|some +-$^%_ multiline input|  - Doubling the argument is trivial with xparse. The problem is that we don't know what the OP really wants. – egreg Feb 17 at 22:04 \frenchspacing should be added to \myverb, as I most of the time have it by default in my documents, I didn't pay attention. – jfbu Feb 26 at 10:55 Note: please, don't vote for this answer, which is actually a large comment that can't be in comments; it is aimed to a clarification of the question With xparse you can define commands that absorb their argument verbatim: \documentclass{article} \usepackage{xparse} \NewDocumentCommand{\verbsagestr}{v}{% \texttt{#1}% } \begin{document} \verbsagestr{Str($1,2_3)}

\end{document}


You can manipulate the argument in various ways, but without any specification it's quite difficult to say more.

The part of your question about verbatim is really unclear.

-
Still need to +1 for making it clear. :) –  herohuyongtao Feb 17 at 22:00
what can we do to stop you getting upvotes? even explicit request is not obeyed! :) –  jfbu Feb 17 at 22:51
I tried to rewrite the verbatim`-paragraph. –  Daniel Krenn Feb 18 at 7:12