close('all')
clear

% followin 2 functions copied from octave print code
% they are used to locate and call octave-svgconvert executable
function cmd = svgconvert ()

  cmd = "";
  svgconvert_binary = __svgconv_binary__();

  fontdir = getenv ("OCTAVE_FONTS_DIR");

  if (isempty (fontdir))
    fontdir = __octave_config_info__ ("octfontsdir");
  endif

  cmd = sprintf ('%s %%s pdf %3.2f "%s" %d %%s', ...
  undo_string_escapes (svgconvert_binary), ...
  get (0, "screenpixelsperinch"), ...
  undo_string_escapes (fullfile (fontdir, "FreeSans.otf")), 1);

endfunction
function bin = __svgconv_binary__ ()

  persistent binary = "";

  if (isempty (binary))
    ## default installation location is the archlib directory
    bindir = getenv ("OCTAVE_ARCHLIBDIR");
    if (isempty (bindir))
      bindir = __octave_config_info__ ("archlibdir");
    endif

    binary = fullfile (bindir, ...
                       ["octave-svgconvert", ...
                        __octave_config_info__("EXEEXT")]);

    if (! exist (binary, "file"))
      binary = "";
    endif
  endif

  bin = binary;

endfunction

% make the plot
x=linspace(-1,1,101);
s = 0.15;
y1 = exp(-(x/s).^2/2);
y2 = 0.75*exp(-((x-0.3)/0.15).^2/2);
hold on
h = fill([x 1 -1],[y1 0 0],'b', ...
  'FaceAlpha',.3);
set(h,'EdgeAlpha',.3);
fill([x 1 -1],[y2 0 0],'r', ...
  'FaceAlpha',.3);
hold off
box on

title('hacked print -dpdflatexstandalone')
print -dpdflatexstandalone curves-latex-fix

% fix

% 1. reduce figure size to 3/4
% hack to get the svg size right
fpos = get(gcf,'position');
set(gcf,'position',[fpos(1:2) fpos(3:4)*3/4])

% 2. print using gl2ps device "svgnotxt"
% and convert to pdf with octave-svgconvert
pipeline = sprintf(svgconvert,'-','curves-latex-fix-inc.pdf');
drawnow ('svgnotxt', ["| " pipeline]);

% 3. run pdflatex
[out, txt] = system(['pdflatex curves-latex-fix.tex']);

% restore figure pos
set(gcf,'position',fpos)



