# Environment with embedded command

I'm trying to define an environment which puts the text in quotation marks - however it contains a cite-command, which should not be manipulated.

Example:

\begin{myEnv}
this text in quotes. \autocite[p.123]{author.2019}
\end{myEnv}


The end-result should look like this:

"this text in quotes." (author.2019, p.123)

Currently this is the definition of myEnv:

\newenvironment{myEnv}{\grqq}{\grqq}


which looks like this:

"this text in quotes. (author.2019, p.123)"

Is there any way to 'exclude' the cite-command from being parsed?

• Does an invocation of your environment always contain a text that you want to be quoted and a cite command? – LokiRagnarok Sep 3 at 12:06
• @LokiRagnarok yes – RazorHail Sep 3 at 12:16

## 3 Answers

This uses a similar approach to LokiRagnarok's answer and assumes you'll always end your myEnv environment with an \autocite (or similar; change as necessary).

\documentclass{article}

\usepackage{biblatex}

\newenvironment{myEnv}
{\let\oldautocite\autocite
\renewcommand{\autocite}{\unskip ''~\oldautocite}%
\ignorespaces}
{\par}

\addbibresource{biblatex-examples.bib}

\begin{document}

\begin{myEnv}
this text in quotes. \autocite[p.~123]{augustine}
\end{myEnv}

\begin{myEnv}
Another text in quotes. \autocite{companion}
\end{myEnv}

\printbibliography

\end{document}

• This is prettier than my answer. Upvoted. (Also, TIL that renewcommand within an environment applies to that environment only, so I needn't have bothered with resetting at the end of the environment...) – LokiRagnarok Sep 4 at 7:30

You can exploit regular expressions:

\documentclass{article}
\usepackage{biblatex}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentEnvironment{myEnv}{b}
{
\tl_set:Nn \l__razorhall_myenv_tl { #1 }
\regex_replace_once:nnN
{ (.*) (\s\c{autocite}.*) \Z }
{ \1''\c{nobreakspace}\2 }
\l__razorhall_myenv_tl
\begin{quote}
\tl_use:N \l__razorhall_myenv_tl
\end{quote}
}{}
\ExplSyntaxOff

\addbibresource{biblatex-examples.bib}

\begin{document}

\begin{myEnv}
this text in quotes. \autocite[p.~123]{augustine}
\end{myEnv}

\begin{myEnv}
Another text in quotes. \autocite{companion}
\end{myEnv}

\printbibliography

\end{document}


Updated for the version where the environment invocation cannot be changed:

\documentclass{article}
\usepackage[ngerman]{babel} %grqq

\usepackage{xpatch}

\newenvironment{myEnv}{%
\let\oldcite\cite %save original macro
\xpretocmd{\cite}{\makebox[0em][r]{\grqq}}{}{}%
%prepend grqq to every cite and gobble a bit of space to the left
% prepend grqq to your TEXT
\grqq}{%
\let\cite\oldcite% %switch back to old version
}

\begin{document}
\begin{myEnv}
first text in quotes. \cite[p.123]{author.2019}
\end{myEnv}

\begin{myEnv}
second text in quotes. \cite[p.123]{author.2019}
\end{myEnv}

This is a simple cite. \cite{new citation}

\begin{myEnv}
third text in quotes. \cite[p.123]{author.2019}
\end{myEnv}
\end{document}



General version:

Use a parameter for your environment through which you pass your citation.

\usepackage{xparse}
....
\NewDocumentEnvironment{myEnv}{ m }{%
\grqq%
}{%
\grqq #1%
}


and use it like this:

\begin{myEnv}{\autocite[p.123]{author.2019}}
this text in quotes.
\end{myEnv}


(Thanks to Phelype Oleinik for finding an error in my solution).

• With \newenvironment you cannot use parameters in the \end part of the environment. You'd need to load xparse and use \NewDocumentEnvironment instead. – Phelype Oleinik Sep 3 at 12:23
• groan Yeah, I actually ran into this issue myself at some point and should know better. – LokiRagnarok Sep 3 at 12:24
• Just one more small nitpick: the m(andatory) argument type should be enclosed in curly braces, so you'd need to use \begin{myEnv}{\autocite...}. If you provide a compilable code (which OP didn't, unfortunately), you can check if your solution works. – Phelype Oleinik Sep 3 at 12:34
• Fixed, thank you. – LokiRagnarok Sep 3 at 12:35
• @LokiRagnarok thanks, but this does notsolve my problem because I cannot change the layout of the text. It's being auto-generated by another program. The format is always: \begin{ENVNAME}TEXT \cite[PAGE]{AUTHOR.YEAR}\end{ENVNAME} – RazorHail Sep 3 at 12:57