1

I'm trying to define a macro with \newcommand using verbatim or listings environment. It seems that the hash key in #1 (standing for the argument) is escaped, due to verbatim and listings.

I'm new to macros, so I tried something simple : it works with \begin{center} ... \end{center}.

\documentclass[a4paper,oneside,11pt]{report}
\newcommand{\script}[1]{
  \begin{center}
    #1
  \end{center}
}
\begin{document}
  \script{blabla}
  blibli
\end{document}

When I replace center with verbatim, I get this error :

File ended while scanning use of @xverbatim.

or lstlisting :

Text dropped after begin of listing

I didn't find anything on stackoverflow nor https://tex.stackexchange.com : what would you advise to use those environments in macros (\newcommand or maybe \newenvironment) ?

Thanks in advance

vvffl
  • 73
  • 1
  • 9
  • 1
    What is the aim here? Verbatim content cannot be passed as an argument to other macros. – Werner Jan 18 '19 at 15:58
  • I'd like 1) to paste scripts and code without having to escape characters 2) make a minipage or sthg else, maybe a box, to distinguish between code and normal text in the page. – vvffl Jan 18 '19 at 16:08
  • By "scripts of code" are you referring to the actual code? I would suggest writing your own environment based on [`listings`](//ctan.org/pkg/listings). – Werner Jan 18 '19 at 16:12
  • I'm making a tutorial in which I use PHP, bash, Sparql. But I'd like to paste any text with special chars. What do you mean with > writing your own environment based on listings. ? Is there a simple manner to do so (haven't found yet)? – vvffl Jan 18 '19 at 16:18
  • Same question on TeX.SE [listings - How to define macro that only makes argument substitution? - TeX - LaTeX Stack Exchange](https://tex.stackexchange.com/questions/42144/how-to-define-macro-that-only-makes-argument-substitution?noredirect=1&lq=1) // Same question on SO: [latex - Using lstlisting environment withing macros? - Stack Overflow](https://stackoverflow.com/questions/6483987/using-lstlisting-environment-withing-macros) – user202729 Nov 15 '21 at 23:57

2 Answers2

3

Verbatim content is tricky. You have to ask yourself what the intent is. If it's printing code, then king of the hill would be listings. I'd suggest that and define your own environment for large chunks of code-specific output.

Here's an example:

enter image description here

\documentclass{article}

\usepackage{listings}

\lstnewenvironment{code}[1][]
  {\lstset{#1}}% Add/update settings locally
  {}

\lstset{% Global options
  frame = single,
  basicstyle = \ttfamily\small,
  language = PHP
}

\begin{document}

My first PHP ``Hello World'' page:

\begin{code}
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?> 
 </body>
</html>
\end{code}

When you write \lstinline!<title>PHP Test</test>!, it sets the \textit{title} of the page.

\end{document}
Werner
  • 14,324
  • 7
  • 55
  • 77
  • Thank you very much Werner, you convinced me to learn more about `listings`. Didn't know of `\lstinline`. Could you just explain the syntax of `{\lstset{#1}}` : does it mean I can add an option as argument? – vvffl Jan 18 '19 at 21:20
  • 1
    @vvffl: Yes. I defined `code` as an environment that can take an optional argument. So you can call (say) `\begin{code}[language = bash]`...`\end{code}` to switch the `language`. It would probably be ideal for you to define a *style* depending on the language (styles include colour and other formatting specifications). – Werner Jan 18 '19 at 21:30
  • Thanks, it's exactly what I thought (I have somewhere in my tex files an old paper where I used custom syntax color for xml, so I'm going to 'grep' that first). So macros aren't that difficult, but the syntax still looks weird for me. – vvffl Jan 18 '19 at 21:35
  • Last question : do you know if there's a way to preserve indentation and prevent wrong spaces (e. g. in your example, before '>') when you copy the code from the pdf output to a text editor? – vvffl Jan 18 '19 at 21:54
  • 1
    @vvffl: This is a common requirement, but it's not well-supported. See [Viewer-independent copyable spaces at the beginning of a line?](https://tex.stackexchange.com/q/148144/5764) The fact that the output (in the resulting PDF) does not include the spaces, they aren't selectable for copy-and-paste. As such, indentation is not preserved. The intent of (La)TeX is to produce beautiful documents and therefore might not fully support the "reverse" where that content is copied/pasted elsewhere. – Werner Jan 18 '19 at 22:00
  • 1
    @vvffl: You can try [Copy-pasting leading whitespace and blank lines in listings package (PDF)](https://tex.stackexchange.com/q/142617/5764), which uses [`accsupp`](//ctan.org/pkg/accsupp). – Werner Jan 18 '19 at 22:00
  • Thank you so much for the tips, it saved a lot of time (browsing latex-stackexchange...) – vvffl Jan 18 '19 at 22:12
0

Found a workaround for verbatim with \verb command, and using tilde as delimiter (if I want to use tilde inside the script I have to use \textasciitilde):

\documentclass{article}

\newcommand{\scr}[1]{
    \begin{minipage}{0.9\textwidth} 
        \fbox{
            \parbox{\textwidth}{            
                \verb~#1~               % <-- HERE
            }
        }
    \end{minipage}  
}   

\begin{document}        
    \scr{Some script code here... 

    here a tilde : \textasciitilde
    }
\end{document}

But nothing for listings...


EDIT : I've just noticed that this workaround doesn't keep the "automatic" character escaping, so it's not what I was looking for. I'd like to be able to paste code without escaping special chars.

vvffl
  • 73
  • 1
  • 9
  • This does not work. Within a `verbatim` setting (either within the `verbatim` environment or `\verb`) macros like `\textasciitilde` are not expanded to their meaning since ``\`` is changed to represent a character. As such, your output should contain `\textasciitilde`, not `~`. – Werner Jan 18 '19 at 15:58
  • Thanks for your reply. Actually it does work on my install... Don't know how nor why! – vvffl Jan 18 '19 at 16:09