Skip to content

Commit

Permalink
Workaround and test script for legendflex legend text
Browse files Browse the repository at this point in the history
  • Loading branch information
SchapplM committed Jan 19, 2023
1 parent 919f9cd commit e49f2bc
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/matlab2tikz.m
Original file line number Diff line number Diff line change
Expand Up @@ -3598,7 +3598,6 @@ case guitypes()
if isempty(type) || strcmpi(type,'hggroup')
type = get(h,'ShapeType'); % Undocumented property valid from 2008a
end

switch type
case 'text'
if is3D
Expand Down Expand Up @@ -3642,13 +3641,17 @@ case guitypes()
set(h,'Units',units,'Position',oldPos)
end
end
% manually add vertical offset (determined empirically). Otherwise text
% from legendflex legends is put too high
pos_corr = pos;
pos_corr(2) = pos(2)-6; % works for font size 10

posString = cell(1,npos);
for ii = 1:npos
posString{ii} = formatDim(pos(ii), fmtUnit);
posString{ii} = formatDim(pos_corr(ii), fmtUnit);
end

posString = sprintf('(%s%s)',type,join(m2t,posString,','));
m2t = disableClippingInCurrentAxes(m2t, pos);
m2t = disableClippingInCurrentAxes(m2t, pos_corr);

end
% ==============================================================================
Expand Down
19 changes: 19 additions & 0 deletions test/legendflex_test/figure_test.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
%% the following commands are needed for some matlab2tikz features
\usetikzlibrary{plotmarks}
\usetikzlibrary{arrows.meta}
\usepgfplotslibrary{patchplots}
\usepackage{grffile}
\usepackage{amsmath}

%% you may also want the following commands
%\pgfplotsset{plot coordinates/math parser=false}
%\newlength\figureheight
%\newlength\figurewidth

\begin{document}
\input{figure.tex}
\end{document}
41 changes: 41 additions & 0 deletions test/legendflex_test/legendflex_bug_minexample.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
% Basic legendflex example from the legendflex repository
% https://github.com/kakearney/legendflex-pkg
% Then save figure with matlab2tikz

clear
clc
close all

figure('color','w');

linespec = [repmat({'r';'b';'g';'c';'m'},2,1), ...
[repmat({'-'}, 5, 1); repmat({'--'}, 5, 1)]];


x = [0 10];
y = (1:10)'*x;
lbl = cellstr(num2str((1:10)'));

% hln = NaN(1,2);
hln(:,2) = plot(x, y);
set(hln(:,2), {'color','linestyle'}, linespec);

[hl(2).leg, hl(2).obj, hl(2).hout, hl(2).mout] = ...
legendflex(hln(1:5,2), lbl(1:5), ...
'anchor', {'nw','nw'}, ...
'buffer', [5 -5], ...
'fontsize',10, ... % has to match document. Otherwise box not wide enough
'xscale',0.5, ...
'title', 'Color');
[hl(3).leg, hl(3).obj, hl(3).hout, hl(3).mout] = ...
legendflex(hln([1 6],2), {'thing 1', 'thing 2'}, ...
'ref', hl(2).leg, ...
'anchor', {'ne','nw'}, ...
'buffer', [5 0], ...
'FontName', 'Times New Roman', ...
'fontsize', 10, ...
'title', 'Line');

% Save figure
matlab2tikz('figure.tex', ...
'strictFontSize', false);

0 comments on commit e49f2bc

Please sign in to comment.