Legend in caption

It may be a bit difficult sometimes to deal with the legend of a figure in a LaTeX document. Depending on the context, you may have to place the legend over the existing curves because of a lack of space. You may also have to place it on the side making the centering of the main content difficult. Some people choose to put the legend in the caption thus discarding the above mentioned difficulties. Below is a solution with pstricks:

documentclass[dvips,11pt,fleqn]{article}
usepackage{lmodern}
usepackage{xcolor}
usepackage{graphicx}
usepackage[top=2cm,bottom=2cm,left=2cm,right=2cm,a4paper]{geometry}

newbox{LegendeA}
savebox{LegendeA}{
(begin{pspicture}(0,0)(0.6,0)
psline[linewidth=0.04,linecolor=red](0,0.1)(0.6,0.1)
end{pspicture})}
newbox{LegendeB}
savebox{LegendeB}{
(begin{pspicture}(0,0)(0.6,0)
psline[linestyle=dashed,dash=1pt 2pt,linewidth=0.04,linecolor=blue](0,0.1)(0.6,0.1)
end{pspicture})}

begin{document}
begin{figure}[h]
centering
begin{pspicture*}[showgrid=true](0,-2.5)(7,2.5)
psframe[fillstyle=solid,fillcolor=gray!10](-1,-3)(8,3)
psplot[linewidth=1.5pt,algebraic=true,linecolor=red]
{0}{7}{sqrt(x)*cos(x)*sin(x)}
psplot[linewidth=1.5pt,algebraic=true,linecolor=blue,linestyle=dashed,dash=1pt 2pt]
{0}{7}{sqrt(x)*cos(x)}
end{pspicture*}
caption{Vibrations of the structure; wheel~usebox{LegendeA} and
piston~usebox{LegendeB}}
end{figure}
end{document}


TeX - LaTeX Asked by pluton on December 27, 2020

At one point I had a situation like you describe. What I ended up doing is putting the legend so that it juts out a little and fiddled with the boundingbox of the resulting figure so that it remains the same as when the legend does not jut out.

I wrote a "script" that steals the BBox of one .eps file and plops it into another:

#!/bin/bash
#take the bounding box of the first argument, and plop it into the second.

BB=grep %%BoundingBox $1 sed "s/%%BoundingBox.*/$BB/" \$2


This script simply outputs the resulting .eps file to stdout. if you want to direct it to a third file you'd use it like so:

% copyBB goodBB.eps niceLegend.eps > combined.eps


(It's a bash script, sorry windows users...but then, it isn't so complicated...so you could re-implement it for your shell.) Caveat Empor! I did not test this for many cases. My use-case was as follows: create two versions of the same figure, one with the correct bounding box, and one with the legend jutting out (upper right corner, in my case). Then, I use the script to steal the BB from the "clean" figure and place it onto the legended one. This allowed me to align figures more easily than otherwise (in my case I also wanted to signal that the same legend can be used for the four figures):

(The lower left figure was created using copyBB, the remaining 3 were unmodified)

Correct answer by Yossi Farjoun on December 27, 2020

You can make rectangles in the text using the rule command.

Example:

rule[raise]{width}{thickness}
({color{red} rule[1.25 pt]{16 pt}{3 pt}})


Answered by Jens on December 27, 2020

Related Questions

Getting my initials in logo style

1  Asked on December 3, 2021

Editing a tabular:

3  Asked on December 3, 2021

Space atop of align should depend on length of preceding line

1  Asked on December 1, 2021

Length of underbracket with Libertinus Math

1  Asked on December 1, 2021

Why does PDFSIZEOPT not reduce file size much?

1  Asked on December 1, 2021

Ideas/Help on Drawing Braid Permutations?

1  Asked on December 1, 2021

unwanted page break in multilanguage document

1  Asked on December 1, 2021

Drawing diagonals of a hexagon with asymptote

1  Asked on December 1, 2021

usepackage{caption} with beamer gives Missing number treated as zero

1  Asked on December 1, 2021

Changing background color of glossaries header row

1  Asked on December 1, 2021

How to include text just in one slide removing spacing using beamer

0  Asked on December 1, 2021

Vertical dots in a tree with forest

1  Asked on December 1, 2021

How to change style of section titles

1  Asked on December 1, 2021

Column and row padding in tables

13  Asked on November 29, 2021

Table markers for apa 6 class

2  Asked on November 29, 2021

topsep, itemsep, partopsep, parsep – what do they each mean (and what about the bottom)?

2  Asked on November 29, 2021

Undefined Control Sequence with imakeidx

2  Asked on November 29, 2021

Wrong font scaling using when groupplot, tikzscale and fillbetween, maybe a bug (?)

0  Asked on November 29, 2021

Why can’t I use @MakeShortVerb with minted?

2  Asked on November 29, 2021

Equation, Onehalfspacing and Underfull vbox

1  Asked on November 29, 2021