[Tex/LaTex] Turn off caption prefix WITHOUT using caption package

captionstufte

How do I remove the caption prefix (i.e Figure 1:, Table 1:) from figures, tables, etc. WITHOUT using the caption package? The reason I don't want to is because I'm using tufte-latex which already redefines the \caption command, so it doesn't work.

EDIT: More specifically, if I choose to use the normal \caption command, the figure is numbered and shows up in the List of Figures. If I use the alternative approach, the figure is not numbered and doesn't show up in the List of Figures. Same applies for tables.

Thanks in advance!

Best Answer

Some surgery on tufte-common.def in order to accommodate for a \varcaption command which has the syntax

\varcaption[<offset>]{<caption text>}

(the optional argument is similar to the second optional argument for \caption).

\documentclass{tufte-book}
\usepackage{regexpatch}

\usepackage{lipsum} % just for the example

\makeatletter
\newcommand{\varcaption}[2][0pt]{%
  \gsetlength{\@tufte@caption@vertical@offset}{-#1}%
  \gdef\@tufte@stored@varcaption{#2}%
}
\xpatchcmd{\end@tufte@float}
  {\par\vspace}
  {\ifthenelse{\NOT\equal{\@tufte@stored@varcaption}{}}{\@tufte@varcaption{\@tufte@stored@varcaption}}{}\par\vspace}
  {}{}
\xapptocmd{\end@tufte@float}{\gdef\@tufte@stored@varcaption{}}{}{}
\gdef\@tufte@stored@varcaption{} % initialize
\newcommand\@tufte@varcaption[1]{%
  \begingroup
  \@parboxrestore 
  \if@minipage\@setminipage\fi
  \@tufte@caption@font
  \@tufte@caption@justification
  \noindent #1\par
  \endgroup
}
\makeatother

\begin{document}

\listoffigures

\chapter{Title}

\lipsum[2]

\begin{figure}
Something
\caption{A numbered caption}
\end{figure}

\lipsum[3]

\begin{figure}
Something
\varcaption{An unnumbered caption}
\end{figure}

\lipsum[4]

\end{document}

You'll see that the second caption is not listed in the List of Figures.

enter image description here

A version without regexpatch; copy in your document preamble everything from \makeatletter to \makeatother (included).

\documentclass{tufte-book}

\usepackage{lipsum} % just for the example

\makeatletter
\newcommand{\varcaption}[2][0pt]{%
  \gsetlength{\@tufte@caption@vertical@offset}{-#1}%
  \gdef\@tufte@stored@varcaption{#2}%
}
\gdef\@tufte@stored@varcaption{} % initialize
\newcommand\@tufte@varcaption[1]{%
  \begingroup
  \@parboxrestore 
  \if@minipage\@setminipage\fi
  \@tufte@caption@font
  \@tufte@caption@justification
  \noindent #1\par
  \endgroup
}
\def\end@tufte@float{%
      \par\hbox{}\vspace{-\baselineskip}\ifthenelse{\prevdepth>0}{\vspace{-\prevdepth}}{}% align baselines of boxes
      \end{minipage}%
    \end{lrbox}%
    % build the caption box
    \begin{lrbox}{\@tufte@caption@box}%
      \begin{minipage}[\floatalignment]{\marginparwidth}\hbox{}%
        \ifthenelse{\NOT\equal{\@tufte@stored@caption}{}}{\@tufte@orig@caption[\@tufte@stored@shortcaption]{\@tufte@stored@caption}}{}%
        \ifthenelse{\NOT\equal{\@tufte@stored@label}{}}{\@tufte@orig@label{\@tufte@stored@label}}{}%
        \ifthenelse{\NOT\equal{\@tufte@stored@varcaption}{}}{\@tufte@varcaption{\@tufte@stored@varcaption}}{}
        \par\vspace{-\prevdepth}%% TODO: DOUBLE-CHECK FOR SAFETY
      \end{minipage}%
    \end{lrbox}%
    % now typeset the stored boxes
    \begin{fullwidth}%
      \begin{minipage}[\floatalignment]{\linewidth}%
        \ifthenelse{\boolean{@tufte@float@star}}%
          {\@tufte@float@fullwidth[\@tufte@caption@vertical@offset]{\@tufte@figure@box}{\@tufte@caption@box}}%
          {\@tufte@float@textwidth[\@tufte@caption@vertical@offset]{\@tufte@figure@box}{\@tufte@caption@box}}%
      \end{minipage}%
    \end{fullwidth}%
    \@tufte@orig@endfloat% end original LaTeX float environment
    % output debug info
    \ifthenelse{\boolean{@tufte@debug}}{%
      \typeout{^^J^^J----------- Tufte-LaTeX float information ----------}%
      \ifthenelse{\equal{\@tufte@stored@label}{}}%
        {\typeout{Warning: Float unlabeled!}}%
        {\typeout{Float label: [\@tufte@stored@label]}}%
      \typeout{Page number: [\thepage]}%
      \def\MessageBreak{^^J}%
      \typeout{\@tufte@float@debug@info}%
      \ifthenelse{\boolean{@tufte@symmetric}}%
        {\typeout{Symmetric: [true]}}%
        {\typeout{Symmetric: [false]}}%
      \typeout{----------------------------------------------------^^J^^J}%
    }{}%
    % reset commands and temp boxes and captions
    \gdef\@tufte@float@debug@info{}%
    \let\caption\@tufte@orig@caption%
    \let\label\@tufte@orig@label%
    \begin{lrbox}{\@tufte@figure@box}\hbox{}\end{lrbox}%
    \begin{lrbox}{\@tufte@caption@box}\hbox{}\end{lrbox}%
    \gdef\@tufte@stored@shortcaption{}%
    \gdef\@tufte@stored@caption{}%
    \gdef\@tufte@stored@label{}%
    \gsetlength{\@tufte@caption@vertical@offset}{0pt}% reset caption offset
    \gdef\@tufte@stored@varcaption{}
  }
\makeatother

\begin{document}

\listoffigures

\chapter{Title}

\lipsum[2]

\begin{figure}
Something
\caption{A numbered caption}
\end{figure}

\lipsum[3]

\begin{figure}
Something
\varcaption{An unnumbered caption}
\end{figure}

\lipsum[4]

\end{document}

Support for marginfigure and margintable

With the following code that must be added to the previous one before the final \makeatother, you can use \varcaption also in marginfigure and margintable.

\renewenvironment{@tufte@margin@float}[2][-1.2ex]%
  {\FloatBarrier% process all floats before this point so the figure/table numbers stay in order.
  \begin{lrbox}{\@tufte@margin@floatbox}%
  \begin{minipage}{\marginparwidth}%
    \renewcommand\varcaption[2][]{\par\@tufte@varcaption{##2}}%
    \@tufte@caption@font%
    \def\@captype{#2}%
    \hbox{}\vspace*{#1}%
    \@tufte@caption@justification%
    \@tufte@margin@par%
    \noindent%
  }
  {\end{minipage}%
  \end{lrbox}%
  \marginpar{\usebox{\@tufte@margin@floatbox}}%
  }