0

If you have a complex tikzpicture with many \addplot commands and \addlegendentry commands, references to many datasets, probably also a colorbar, and so on: is there a convenient way to only hide/show the legend or only hide/show the colorbar, or to hide all the plot lines while keeping the legend?

I want to avoid having to uncomment and edit many things and there is for example a very convenient way to hide the axis by using hide axis, somewhere within the axis options of the tikzpicture. I am looking for similar commands to hide the legend or to hide the colorbar or to hide the actual line data (plots) while still showing the correct legend (with correct colors, entries etc.).

The goal is for example to be able to create a pdf, which just shows the cropped legend (with all the correct lines in the corresponding colors of the plots and with the correct legend entries) but hides all of the rest of the figure's elements (colorbar, axis, plot lines...). Is something like this possible? I would like to avoid to create the legend manually as mentioned here: 54794

phw
  • 343
  • 1
    I am not sure if I have understood you right, but most stuff is only drawn when you use corresponding commands. So if you would e.g. use \legend (inside the axis environment) or legend entries (in the axis options) this could easily be commented to "hide" it. The same is true for colorbar. But I don't see the point to draw some stuff in a legend when it is not shown in the plot. Could you edit your question to let us know for what that should be good for? Maybe then it easier to think of a solution. – Stefan Pinnow Sep 22 '16 at 19:01
  • You got it right... actually the reason is that I get the output from matlab2tikz and I don't want to overwrite the whole code (deleting or uncommenting all plots, etc.), just to realize a "legend only" export. Why I'd like to have a legend only doesn't matter :-). But the basic question is: is it possible to get the corresponding legend (which appearance is based on the plots added and their colors, marks, etc.) without actually having to show the plots/axis/etc - to get a legend only output. – phw Sep 23 '16 at 08:35
  • The only way I could think of is using the legend to name key which "stores" the legend and can be \refed somewhere else in the document. But that only isn't enough, because when you comment the plot of course the "\label" will also be gone. But in addition you could use the external library to externalize the legend to a (separate) PDF file which then can be included into the document. After the externalization you don't need the plot any more. Is that an option? – Stefan Pinnow Sep 23 '16 at 18:07
  • Actually I don't fully understand it. In how far would it create a PDF, which only contains the legend, then? – phw Sep 23 '16 at 18:28

1 Answers1

3

As I already stated in the comments below the question, you can externalize the legend to a PDF. For that have a look at the following example (including the comments).

% use this document to externalize the legend of the plot
% (it is important that the `--shell-escape' feature is enabled)
\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
    % load `external' library from PGFPlots and not TikZ,
    % because its newer and some bugs are fixed
    \usetikzlibrary{
        pgfplots.external,
    }
    % when you are new to LaTeX/PGFPlots then you should use
    % the newest `compat' level to use all new features
    \pgfplotsset{
        compat=newest,
    }
    % activate externalization
    \tikzexternalize[
        % with the following key you can define where to store the
        % externalized files. Otherwise they will be stored in the
        % main/\jobname folder
        prefix=Pics/pgf-export/,
        % only externalize stuff that is "named" with `\tikzsetnextfilename'
        only named=true,
%        % if you have to force to reexternalize stuff, uncomment the next line
%        force remake,
    ]
\begin{document}
% if you (also) want to externalize the plot, give him a name
% (uncomment the next line)
%    \tikzsetnextfilename{linear_plots}
    \begin{tikzpicture}
        \begin{axis}[
            legend columns=-1,
            legend entries={
                $(x+0)^k$;,
                $(x+1)^k$;,
                $(x+2)^k$;,
                $(x+3)^k$
            },
            legend to name=legend:linear_plots,
        ]
            \addplot {x};
            \addplot {x+1};
            \addplot {x+2};
            \addplot {x+3};
        \end{axis}
    \end{tikzpicture}

    bla
    % to externalize the (external) legend, give it a name with the
    % following command
    \tikzsetnextfilename{linear_plots_legend}
    % then reference the legend, so it is drawm and thus externalized
    \ref{legend:linear_plots}
    blub
\end{document}

Then you can simply use \includegraphics in your real document to show the legend anywhere you want.

% in your "real" document you can then include the externalized plot/legend
\documentclass[border=2mm]{standalone}
\usepackage{graphicx}
    % also add the externalize path (from above)
    % to the `\graphicspath'
    \graphicspath{{Pics/pgf-export/}}
\begin{document}
    bla \includegraphics{linear_plots_legend} blub
\end{document}
phw
  • 343
Stefan Pinnow
  • 29,535
  • This is totally neat! It solved my problem in any way. I just realized that this XYZ to name feature does not only work for a legend, but as well for a colorbar! And I can realize to have a pdf with the axis only (including plots) as well as a seperated pdf with the legend only / respectivly the colorbar only (as typically one would only have one of these) or both in seperate pdfs (if one should want a legend and a colorbar at the same time) - and it only needs one single compilation!. Thank you very much. – phw Sep 27 '16 at 05:09
  • You are welcome. Just to make it clear: The normal use of the external library is to externalize stuff in the "real" document directly by using the preamble and commands of the upper code. Using \includegraphcs to reuse the externalized stuff is a very special case if you don't need the plot which normally doesn't make any sense. (That was why I was asking further questions.) – Stefan Pinnow Sep 27 '16 at 05:34