Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The following code gives me an error:

\documentclass[ngerman]{article}
\usepackage{babel,csquotes}
\begin{document}
\enquote{\verb|foo|}
\end{document}

I guess this is the classic "you can't use verbatim material in commands" problem. Is there a solution to this besides writing \texttt{foo} or using quotation marks directly?

share|improve this question

4 Answers 4

up vote 10 down vote accepted

I wrote the newverbs package with such an application in mind, i.e.\ quote a verbatim text. It provides, beside other things, a \qverb macro for quoted verbatim material. It is compatible with csquotes and will us its start and end quote marks.

\documentclass[ngerman]{article}
\usepackage{babel,csquotes}
\usepackage{newverbs}% load after csquotes

% If you want to have automatic nested quotes.
% By default single (inner) quotes are used.
\let\qverbbeginquote\openautoquote
\let\qverbendquote\closeautoquote
% or
%\renewverbcommand{\qverb}{\openautoquote}{\closeautoquote}
\begin{document}
\qverb|foo|

%Compare
\enquote{\texttt{foo}}
\end{document}

Result


If you want to support quoted normal text which includes some verbatim material you could either use \openautoquote .. \closeautoquote directly, or use a brace group and a macro like this to place the closing quote at the end of the group:

\documentclass[ngerman]{article}
\usepackage{babel,csquotes}
\newcommand{\genquote}{\openautoquote\bgroup\aftergroup\closeautoquote\aftergroup\egroup}
\begin{document}
{\genquote This is a longer text.

It also include verbatim \verb|foo|.
Etc.}
\end{document}

This is also possible in (pseudo-)macro form:

\documentclass[ngerman]{article}
\usepackage{babel,csquotes}
\newcommand{\genquote}{\openautoquote\bgroup\aftergroup\closeautoquote\let\dummy= }
\begin{document}
\genquote{This is a longer text.

It also include verbatim \verb|foo|.
Etc.}
\end{document}

(Instead of \dummy the internal \@let@token can be used; then you don't "waste" a macro name.)

share|improve this answer
    
I would vote for making \let\qverbbeginquote\openautoquote and the other default. –  topskip Jun 6 '12 at 8:48
    
@PatrickGundlach: I was thinking just about this. Originally I used single quotes for verbatim, especially very short ones (1-2 characters) where the double quotes looked to big. However, having the normal quotes as default seem to be more correct. Now, I'm always skeptical about changing the default value of one of my packages. I have to think about it. –  Martin Scharrer Jun 6 '12 at 8:51
1  
This is of course the easiest way when only \verb|foo| needs to be quoted. If the verbatim bit is embedded in a phrase to be quoted, then the environment form seems to be the only way (unless one is satisfied with "saving" the verbatim bit beforehand). –  egreg Jun 6 '12 at 9:01
    
@egreg: You are totally right. I figured from the example that only a verbatim text should be quotes; especially because I needed this very often in the past in my package manuals. If you need to quote longer text with verbatim included I would use \openautoquote and \closeautoquote directly instead of defining and using an environment. –  Martin Scharrer Jun 6 '12 at 9:05
    
@MartinScharrer Good idea. However there was a missing final space and I'd prefer \def\genquote#{...} so that a brace is required: \genquote x would lead to very obscure errors. –  egreg Jun 6 '12 at 9:25
\documentclass[ngerman]{article}
\usepackage{babel,csquotes,listings}
\begin{document}
\enquote{\lstinline|foo|}
\end{document}
share|improve this answer

Without using any packages, a general mechanism for using verbatim material in a context where \verb is not allowed is

\setbox0\hbox{\verb|fooo|}

\enquote{\usebox0}

Note you have to use \setbox here not the LaTeX \sbox or \savebox as they read their argument as macro argument so \verb is not allowed.

share|improve this answer
    
There are the collectbox and realboxes packages which would make this easier, but defeat the purpose of not using a package again. –  Martin Scharrer Jun 6 '12 at 9:02

Here's a "poor man's" environment form (thanks to Martin Scharrer for pointing to the \openautoquote command):

\documentclass[ngerman]{article}
\usepackage{babel,csquotes}

\newenvironment{inlineenquote}
  {\openautoquote\ignorespaces}
  {\unskip\closeautoquote}
\newenvironment{inlineenquote*}
  {\openinnerquote\ignorespaces}
  {\unskip\closeinnerquote}

\begin{document}

Abc \enquote{XXX} def \enquote*{XXX} ghi

Abc
\begin{inlineenquote}
\verb|XXX|
\end{inlineenquote}
def
\begin{inlineenquote*}
\verb|XXX|
\end{inlineenquote*}
ghi

\end{document}

One may find this easier to manage than writing

Abc \openautoquote\verb|XXX|\closeautoquote{} def

Of course these two forms are more practical over Martin's \qverb when the verbatim material is embedded in a phrase and not the only thing to be quoted.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.