6

I currently have a quote in my tex file like this:

\begin{quote}
blabla
\end{quote}

Now I want to refer to this quote inline. Adding a label works but I cannot get the caption to work, in order to show that this quote is actually number x.y. As this environment does not seem to be a float environment, which is apparently a requirement for \caption{}.

So I have tried the following:

\begin{quote}
blabla
\label{quote:one}
\end{quote}

And addint \caption{test} right before the label does not work.

lockstep
  • 250,273
Someguy
  • 345
  • 6
    A quote environment does not have a counter, therefore labelling is useless, in this case. Your request requires some setup ;-) –  Feb 23 '16 at 14:37
  • The accepted solution is defective. See the answer at https://tex.stackexchange.com/a/563217/202780 for a simpler and more flexible solution. – Don Hosek Sep 18 '20 at 15:47

3 Answers3

6

See below for Alternate Appearance. See ADDENDUM for version that uses aux file to allow future referencing

As Christian noted in a comment, a quote has no counter (and thus no identifying label). However, we could introduce one to the lquote environment, which requires a label as the argument, reference-able by \quoteref.

The implementation given below can be altered to suit the needs for quotation marks, numbering scheme and appearance, etc.

\documentclass{article}
\newcounter{numquote}
\newenvironment{lquote}[1]{%
  \stepcounter{numquote}%
  \expandafter\xdef\csname#1\endcsname{\fbox{\thenumquote}}%
  \quote``\ignorespaces}{\unskip''\fbox{\thenumquote}\endquote}
\newcommand\quoteref[1]{\csname#1\endcsname}
\begin{document}
Compare this quote
\begin{lquote}{quote:one}
blabla
\end{lquote}
to this one
\begin{lquote}{quote:two}
moreblabla
\end{lquote}
In quotation \quoteref{quote:one}, we see a difference from quote \quoteref{quote:two}.
\end{document}

enter image description here

Alternate Appearance

\documentclass{article}
\newcounter{numquote}
\newenvironment{lquote}[1]{%
  \stepcounter{numquote}
  \expandafter\xdef\csname#1\endcsname{\thenumquote}%
  \quote Quote \thenumquote: ``\ignorespaces}{\unskip''\endquote}
\newcommand\quoteref[1]{\csname#1\endcsname}
\begin{document}
Compare this quote
\begin{lquote}{quote:one}
blabla
\end{lquote}
to this one
\begin{lquote}{quote:two}
moreblabla
\end{lquote}
In quotation \quoteref{quote:one}, we see a difference from quote \quoteref{quote:two}.
\end{document}

enter image description here

ADDENDUM:

It seemed from the OP's comment that he may be using the \quoteref prior to the label definition in the lquote environment. To account for that, I have in this addendum implemented a system of writing out to the aux file to make the labels available even before they are defined.

\documentclass{article}
\makeatletter
\long\def \protected@iwrite#1#2#3{%
     \begingroup
     \let\thepage\relax
     #2%
     \let\protect\@unexpandable@protect
     \edef\reserved@a{\immediate\write#1{#3}}%
     \reserved@a
     \endgroup
     \if@nobreak\ifvmode\nobreak\fi\fi
    }
\newcounter{numquote}
\newenvironment{lquote}[1]{%
  \stepcounter{numquote}%
  \protected@iwrite\@auxout{\def\nex{\noexpand\noexpand\noexpand}}{%
    \nex\expandafter\xdef%
    \nex\csname #1%
    \nex\endcsname{\thenumquote}%
  }%
  \quote Quote \thenumquote: ``\ignorespaces}{\unskip''\endquote}
\makeatother
\newcommand\quoteref[1]{\csname#1\endcsname}
\begin{document}
In the future quotation \quoteref{quote:one}, 
  we see a difference from quote \quoteref{quote:two}.

Compare this quote
\begin{lquote}{quote:one}
blabla
\end{lquote}
to this one
\begin{lquote}{quote:two}
moreblabla
\end{lquote}
In quotation \quoteref{quote:one}, we see a difference from quote \quoteref{quote:two}.

\end{document}

enter image description here


Note: the \protected@iwrite macro came from egreg's answer at Writing \\ to a File.

  • At least one that takes note of my comments ;-) –  Feb 23 '16 at 15:01
  • Tried this but it does not show my a reference in the inline quote when writing \quoteref{quote:one}. Nothing appears there. Also, how can I make it looking more like the default captions? Not this "boxed" [1] but like "Quote x.y: blabla" below the quote. – Someguy Feb 23 '16 at 15:03
  • 1
    @Someguy I have provided something more in line with your comments. – Steven B. Segletes Feb 23 '16 at 15:12
  • Looks good, thanks! Except it still does not show anything in the reference itself. I wrote (see Figure \quoteref{quote:one}) but it only appears as (see Figure ). – Someguy Feb 23 '16 at 18:00
  • @Someguy I can only reproduce that result if I use \quoteref before the lquote, because the quoteref label is not yet defined. In order to make the label accessible before the quote, you need a much more more elaborate system that writes things out to the aux file, – Steven B. Segletes Feb 23 '16 at 18:06
  • 1
    @Someguy I have added an addendum to make use of the aux file to allow you to use \quoteref before the invocation of lquote. – Steven B. Segletes Feb 23 '16 at 18:56
  • I kinda figured it would be because I had the quote after the reference :) Thank you, it works now! – Someguy Feb 23 '16 at 20:11
  • The reference appears in the text, but there is no link for me to click on the number and jump to the quote referenced, how can I fix this? – Lucas Azevedo Sep 18 '20 at 14:21
  • 1
    @LucasAzevedo I have no quick answer for you. Perhaps this could be asked as a new Question on the site. – Steven B. Segletes Sep 18 '20 at 14:28
  • @LucasAzevedo However, note the comment to this original question: "A quote environment does not have a counter, therefore labelling is useless, in this case. Your request requires some setup". This is an essential hindrance in trying to establish a hyperlink. – Steven B. Segletes Sep 18 '20 at 14:30
  • @StevenB.Segletes i just did it https://tex.stackexchange.com/questions/563198/how-to-reference-a-quote-environment – Lucas Azevedo Sep 18 '20 at 14:55
  • 1
    @LucasAzevedo Yes, and I've already upvoted it. – Steven B. Segletes Sep 18 '20 at 14:55
  • This is defective in many ways. See the answer at https://tex.stackexchange.com/a/563217/202780 for a simpler and more flexible solution. – Don Hosek Sep 18 '20 at 15:46
  • @DonHosek Glad you could assist. Perhaps you could improve your referenced answer by providing a full MWE. – Steven B. Segletes Sep 18 '20 at 15:49
1

Only a \pageref makes sense:

\documentclass{article}
\usepackage{caption}
\begin{document}

\begingroup
\begin{quote}
    blabla
\end{quote} 
\captionof*{figure}{Test}\label{quote:one}
\endgroup

See quote on page~\pageref{quote:one}
\end{document}
1

The accepted solution is defective in a lot of ways. LaTeX already has its own referencing mechanism and that bypasses it which causes problems as you've seen.

Instead, I would define the lquote environment as follows:

\newcounter{numquote}
\newenvironment{lquote}{%
  \refstepcounter{numquote}%
  \quote}{\unskip~\thenumquote\endquote}

You would use this by something like

\begin{lquote}
  blah blah
  \label{quote:one}
\end{lquote}
...
quote~\ref{quote:one}.

Then, if you put

\usepackage{hyperref}

at the end of your preamble you'll automatically get the desired result with hyperlinks on the reference.

Don Hosek
  • 14,078