-1

I have this figure

enter image description here

\begin{figure}
    \centering
    \captionsetup[subfigure]{labelformat=empty}
    \includegraphics[width=\textwidth]{Figure_1.pdf}
    \caption{}
\end{figure}

I want to draw two lines above it and put a text in the middle of the lines like the figure below.

enter image description here

I tried using the \draw function but it is impossible to have the lines I want. How do I do it ?

\begin{figure}
    \centering
    \captionsetup[subfigure]{labelformat=empty}
    \begin{tikzpicture}
    \draw (-10,0) -- (3,0);
    \end{tikzpicture}
    \includegraphics[width=\textwidth]{Figure_1.pdf}
    \caption{}
\end{figure}
1
  • Does the image file contain all six diagrams?
    – egreg
    Commented Jun 11, 2019 at 14:08

2 Answers 2

3

like this?

\documentclass{article}

\usepackage{graphicx}
\usepackage{subcaption}

\begin{document}

\begin{figure}
    \centering
    \captionsetup[subfigure]{labelformat=empty}
%    \begin{tikzpicture}
%    \draw (-10,0) -- (3,0);
%    \end{tikzpicture}
        \hspace{0.75cm}
        \begin{minipage}{.78\textwidth}
            \centering
            text

            \rule{\textwidth}{2pt}
        \end{minipage}\hfill%
        \begin{minipage}{.1\textwidth}
            \centering
            text

            \rule{\textwidth}{2pt}
        \end{minipage}%
        \hspace{0.2cm}
        \vskip0.5em
    \includegraphics[width=\textwidth]{NpPlW}
    \caption{}
\end{figure}

\end{document}

enter image description here

2
  • The % isn't necessary at the end of a line ending with \hfill, since this command has only letters in its name. However, you should put one after \hspace{0.75cm}, where it is used at the end of a line. Then you will notice that without this extraneous space that you had, \hspace does nothing at the end of a line or of a paragraph. At this point, replace these \hspace calls with \hspace* and you should be good. The reason your end-of-paragraph \hspace commands work is because they are followed in your source by a space token (caused by the return to new line in source file)...
    – frougon
    Commented Jun 11, 2019 at 12:43
  • ... and TeX implicitly does an \unskip at the end of each paragraph, which removes this space token and leaves your \hspace command. So, this is a rather convoluted way of leaving space at the end of a paragraph; I'd suggest to use something like \hspace*{0.2cm}\par, which seems more straightforward to me.
    – frougon
    Commented Jun 11, 2019 at 12:55
0

Here is one way using TikZ, offering a bunch of knobs for easy customization and rounded end points for the horizontal rules (this can be changed if desired, of course). This will be very convenient if you have several such legends to add, because for each legend, you only need to enter something like this:

\draw[legend=from (startpoint) to (endpoint)
      with offset \myRuleOffset and width \myRuleWidth node somenodename
      label text {Some text} offset by \myLabelOffset
      rule options {fill=blue!40} label options {color=red!20!black}];

The complete example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc, fit}

\begin{document}

\begin{figure}
  \centering
  \begin{tikzpicture}[
      legend/.style args={from #1to #2with offset #3and width #4node #5label
                          text #6 offset by #7rule options #8 label options #9}{%
        insert path={
          let \p1 = ($#1 + (0,#3) + (0,#4)$),
              \p2 = ($#2 + (0,#3)$)
          in
          node[inner sep=0, fit=(\p1) (\p2), rounded corners=0.5*#4,
               #8, label={[label distance=#7, anchor=base, #9]above:{#6}}]
            (#5) {}
          }}]

    % Parameters shared between our two legends
    \def\myRuleOffset{0.5cm}
    \def\myRuleWidth{2pt}
    \def\myLabelOffset{0.25cm}

    % The image node
    \node[inner sep=0] (img)
      { \includegraphics[width=\linewidth]{example-image} };

    % The left legend stops a bit before two thirds of the image width
    \coordinate (rightEndOfLeftLegend) at
      ($ (img.north west)!0.65!(img.north east) $);
    % The right legends starts a bit after two thirds of the image width
    \coordinate (leftEndOfRightLegend) at
      ($ (img.north west)!0.68!(img.north east) $);

    % Left legend (creates a rectangle-shaped node named 'leftrule')
    \draw[legend=from (img.north west) to (rightEndOfLeftLegend)
          with offset \myRuleOffset and width \myRuleWidth node leftrule
          label text {Left label} offset by \myLabelOffset
          rule options {fill=blue!40} label options {color=red!20!black}];
    % Right legend (creates a rectangle-shaped node named 'rightrule')
    \draw[legend=from (leftEndOfRightLegend) to (img.north east)
          with offset \myRuleOffset and width \myRuleWidth node rightrule
          label text {Right label} offset by \myLabelOffset
          rule options {fill=orange!40} label options {color=red!20!black}];
  \end{tikzpicture}
  \caption{Your caption here}
\end{figure}

\end{document}

Screenshot

Zoom on two of the rule end points:

Rule end points (zoomed)

Of course, the above code doesn't depend on any figure environment—this environment is only used in the example for presentation purposes.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .