TransWikia.com

adding triangle in caption to point to figure (tufte class)

TeX - LaTeX Asked by adl on December 9, 2020

I’m using the Tufte class for a document with lots of figures, and some of my readers have complained that it is not always easy to locate the caption of figures that are not in the margin. So I’d like to define (and then use) variants the caption macro display a small triangle in front of the caption, pointing to the figure. For instance captiondown{my caption} would display "▼ Figure 3: my caption".

It’s not clear to me how to write such macros. I thought I could achieve that by redefining figurename, but looking at the example below it works in the marginfigure environment, not in the figure and figure* environments. Can someone help?

documentclass[a4paper,nobib]{tufte-handout}
usepackage{amssymb}
usepackage{tikz}
usepackage{showframe}

renewcommand{figurename}{textbf{Figure}}

begin{document}
begin{marginfigure}
  tikzfill[cyan] rectangle(marginparwidth,2);
  renewcommand{figurename}{$blacktriangle$~textbf{Figure}}
  caption{Some caption for the blue figure.}
end{marginfigure}
begin{figure}
  tikzfill[yellow]rectangle(textwidth,4);
  renewcommand{figurename}{$blacktriangleleft$~textbf{Figure}} %ignored?
  caption[][3cm]{Some caption for the yellow figure.}
end{figure}
begin{figure*}
  tikzfill[magenta]rectangle(textwidth,3);
  renewcommand{figurename}{$blacktriangledown$~textbf{Figure}} %ignored?
  caption[][-4.5cm]{Some caption for the magenta figure.}
end{figure*}
end{document}

output

Update:

Here is a variant where I redefine fnum@figure instead. I get the desired output if I redefine that macro before the figure and figure* environment, so I guess I’ll make do with that.

I’d still very much prefer to be able to call captiondown{my caption} inside those environments, but I do not know how to do that.

documentclass[a4paper,nobib]{tufte-handout}
usepackage{amssymb}
usepackage{tikz}
usepackage{showframe}

makeatletter
newcommand{captionsbelow}{%
  renewcommand{fnum@figure}{$blacktriangle$~textbf{figurename}~thefigure}}
newcommand{captionsright}{%
  renewcommand{fnum@figure}{$blacktriangleleft$~textbf{figurename}~thefigure}}
newcommand{captionsabove}{%
  renewcommand{fnum@figure}{$blacktriangledown$~textbf{figurename}~thefigure}}
makeatother

begin{document}
captionsbelow
begin{marginfigure}
  tikzfill[cyan] rectangle(marginparwidth,2);
  caption{Some caption for the blue figure.}
end{marginfigure}
captionsright
begin{figure}
  tikzfill[yellow]rectangle(textwidth,4);
  caption[][3cm]{Some caption for the yellow figure.}
end{figure}
captionsabove
begin{figure*}
  tikzfill[magenta]rectangle(textwidth,3);
  caption[][-4.5cm]{Some caption for the magenta figure.}
end{figure*}
end{document}

output

Final update:

Using all the input I got, a managed to patch the Tufte definitions in a way that the orientation is redefined in each float according to the location of the figure. (I’m just not bothering with recto-verso mode, which I don’t use, but extending that to support it should not be a problem.)

documentclass[a4paper,nobib]{tufte-handout}
usepackage{showframe}
usepackage{amssymb}
usepackage{tikz}
usepackage{etoolbox}

makeatletter
renewcommand{fnum@figure}{captiontriangle~textbf{figurename}~thefigure}
newcommand{captionbelow}{gdefcaptiontriangle{$blacktriangle$}}
newcommand{captionright}{gdefcaptiontriangle{$blacktriangleleft$}}
newcommand{captionabove}{gdefcaptiontriangle{$blacktriangledown$}}
% Use captionbelow by default, for margin figures
AtBeginDocument{captionbelow}
AfterEndEnvironment{@tufte@float}{captionbelow}
% automatically change the triangle for other captions
AtEndEnvironment{@tufte@float}{%
  ifthenelse{boolean{@tufte@float@star}}%
    {ifthenelse{equal{floatalignment}{b}}{captionabove}{captionbelow}}%
    {captionright}}
makeatother

setcounter{totalnumber}{4}% for demonstration
begin{document}
begin{marginfigure}
  tikzfill[cyan] rectangle(marginparwidth,2);
  caption{Some caption for the blue figure.}
end{marginfigure}
begin{figure}
  tikzfill[yellow]rectangle(textwidth,4);
  caption[][3cm]{Some caption for the yellow figure.}
end{figure}
begin{figure*}
  tikzfill[magenta]rectangle(textwidth,3);
  setfloatalignment{b} %force caption above
  caption{Some caption for the magenta figure.}
end{figure*}
begin{figure*}
  tikzfill[green!30]rectangle(textwidth,3);
  caption[][-1.2em]{Some caption for the green figure.}
end{figure*}
begin{figure*}[b]
  tikzfill[gray!30]rectangle(textwidth,3);
  caption{Some caption for the gray figure.}
end{figure*}
end{document}

enter image description here

One Answer

A good workaround for what is happening is to define a command which resets the label of a figure. Afterwards, employ AfterEndEnvironment (from etoolbox) to execute such command after every figure and figure* environment. This way, you might be able to redefine how caption should look like but also still get the original definition of figures caption after the environment ends.

documentclass[a4paper,nobib]{tufte-handout}
usepackage{amssymb}
usepackage{tikz}
usepackage{showframe}

usepackage{etoolbox}

makeatletter
    renewcommand{fnum@figure}{textbf{figurename}~thefigure}
    % save older version of fnum@figure
    let fnum@figureold fnum@figure
    % define a command to reset to the older version
    newcommand{resetfnumfigure}{let fnum@figure fnum@figureold}
makeatother

AfterEndEnvironment{figure}{resetfnumfigure}
AfterEndEnvironment{figure*}{resetfnumfigure}


makeatletter
    newcommand{captionsbelow}{%
      renewcommand{fnum@figure}{$blacktriangle$~textbf{figurename}~thefigure}}
    newcommand{captionsright}{%
      renewcommand{fnum@figure}{$blacktriangleleft$~textbf{figurename}~thefigure}}
    newcommand{captionsabove}{%
      renewcommand{fnum@figure}{$blacktriangledown$~textbf{figurename}~thefigure}}
makeatother

begin{document}

begin{marginfigure}
    captionsbelow
  tikzfill[cyan] rectangle(marginparwidth,2);
  caption{Some caption for the blue figure.}
end{marginfigure}
captionsright
begin{figure}
  tikzfill[yellow]rectangle(textwidth,4);
  caption[][3cm]{Some caption for the yellow figure.}
end{figure}
captionsabove
begin{figure*}
  tikzfill[magenta]rectangle(textwidth,3);
  caption[][-4.5cm]{Some caption for the magenta figure.}
end{figure*}

end{document}

Correct answer by Al-Motasem Aldaoudeyeh on December 9, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP