Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc additions and improvemetns for Inkscape => PDF+LaTeX workflow #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions example/demo_3d_plot2svg.m
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
%% 3D sphere with alpha data
clear all;
figure
[x y z] = sphere(20);
s = surface(x,y,z,'facecolor','interp','cdata',z);
Expand All @@ -12,3 +14,38 @@
zlabel('Z');
title('Sphere with Alpha Data');
plot2svg('sphere.svg');

%% Rescaling patches to avoid gaps
clear all;

x = -3.5:0.1:3.5;
y = -3.5:0.1:3.5;

[X, Y] = meshgrid(x, y);

Z = peaks(X, Y);

figure(1);
clf;
surf(X, Y, Z);
axis tight;
xlabel('x');
ylabel('y');
zlabel('z');
shading flat;
view([20, 80]);

% When exporting 3D plots with flat shading (no mesh lines around the
% patches) some viewers show small gaps between the patches. This can even
% lead to a mind of "semi transparent" appearance with many small patches,
% because the background can be seen in the gaps.
setting.svg.PatchRescale = 1; % This is the deault setting.
set(gcf, 'UserData', setting);
plot2svg('flat_default.svg');

% A simple solution to this problem is to tell plot2svg to slightly
% increase the patch size, such that the patches overlap. A rescaling value
% of 105% is usually a good value to start with.
setting.svg.PatchRescale = 1.05;
set(gcf, 'UserData', setting);
plot2svg('flat_rescaled.svg');
89 changes: 89 additions & 0 deletions example/demo_misc.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
%% Image overlay and typical usage.
% The purpose of the OverlayImage function is to replace parts of a figure,
% e.g., a rendering which is too complex to be stored as vector graphics,
% with a bitmap image. The bitmap file is linked and not embedded in the SVG.

clear all;

% First, render the 3D graphics into a bitmap file.
x = -3.5:0.1:3.5;
y = -3.5:0.1:3.5;

[X, Y] = meshgrid(x, y);

Z = peaks(X, Y);

figure(1);
clf;
surf(X, Y, Z);
axis tight;
xlabel('x');
ylabel('y');
zlabel('z');
shading interp;
lighting('phong');
camlight('headlight');
colormap(jet(512));
axis off;
xl = xlim();
yl = ylim();
zl = zlim();

bg = get(gcf, 'color');
% Make sure the background color is saved, so that we can set it transparent
% later.
set(gcf, 'InvertHardCopy', 'off');

print -dpng -r300 'render.png'

% Load and re-save the image with transparent background.
% Be careful that the background color doesn't occur in your plot. If this
% is the case you should set another background color above.
A = imread('render.png');
imwrite(A, 'render_t.png', 'png', 'transparency', bg);

% Now recreate the same axis without the data.
% Please note:
% In some cases it can be difficult to achieve a correct alignment of the
% bitmap and vector output. It may require some fiddling and your mileage
% may vary.
figure(2);
clf;
surf([0, 0;0, 0], [0, 0;0, 0], [0, 0;0, 0]);
axis tight;
xlabel('x');
ylabel('y');
zlabel('z');
xlim(xl);
ylim(yl);
zlim(zl);

% Last step: Store axis plot as SVG image and use the rendered data as
% overlay.
setting.svg.OverlayImage = 'render_t.png';
setting.svg.OverlayImagePos = [0, 0, 1, 1];
set(gcf, 'UserData', setting);

plot2svg('overlay.svg');

%% Simple use case:
% Of course, you can use the overlay function to just add a bitmap to your
% SVG file.
clear all;

x = 0:0.1:5;
y = 1 + x.^2;

figure(3);
clf;

plot(x, y);
xlabel('x values');
ylabel('y values');

setting.svg.OverlayImage = 'water_stones.jpg';
setting.svg.OverlayImagePos = [0.2, 0.2, 0.25, 0.25];
set(gcf, 'UserData', setting);

plot2svg('overlay_simple.svg');

28 changes: 28 additions & 0 deletions example/inkscape_latex/inkscape_demo.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
\documentclass[a4paper]{article}
\usepackage{graphicx}
%\usepackage{showframe}
\usepackage{geometry}
\usepackage{color}
\usepackage{subfig}

\geometry{a4paper, portrait, left=1.5cm, right=1.5cm, top=1.5cm, bottom=1.5cm, includefoot, includehead}

\newcommand{\includesvg}[1]{\input{#1.pdf_tex}}
\newcommand{\includesvgfn}[1]{\footnotesize\input{#1.pdf_tex}}

\begin{document}
\pagestyle{empty}
\begin{figure}
\centering
\includesvg{demo_graphics}
\caption{Demo for including graphics using \texttt{plot2svg} and \texttt{inkscape}.}
\end{figure}

\begin{figure}
\centering
\hfill\subfloat[Smaller graphics with default font size.]{\includesvg{demo_graphics_smaller}}\hfill\hfill
\subfloat[Smaller graphics with smaller font size.]{\includesvgfn{demo_graphics_smaller}}\hspace*{\fill}
\caption{Since all \LaTeX{} code is overlaid during the document compile pass, the font size can be set in the main document.}
\end{figure}

\end{document}
48 changes: 48 additions & 0 deletions example/inkscape_latex/tutorial_inkscape.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
clear all;
%%

% Generate a nice plot.
dt = 0.01;
tend = 10;
t = 0:dt:tend;

y = sin(2*pi*t);

figure(1);
clf;
plot(t, y);
grid on;
axis tight;
xlabel('$t$ / s');
ylabel('$y$ / V');
set(gca, 'XScale', 'log');

text(0.02, -0.25, '\LaTeX{} test $\frac{a}{b}$');
legend('$\sin(2\pi t)$');

% We want to use the LaTeX + PDF export capability of Inkscape. Therefore,
% we have to make sure that plot2svg preserves all our LaTeX strings.
% This can be achieved by setting the option LatexPassOn to true.
setting.svg.LatexPassOn = true;
set(gcf, 'UserData', setting);

plot2svg('demo_graphics.svg');

% Call Inkscape to convert the SVG file to a PDF and a LaTeX overlay file.
% Note: This works only if the path to the Inkscape executable is correctly
% set up. Alternatively you could do this manually in Inkscape.
system('inkscape -z --export-area-page --file=demo_graphics.svg --export-pdf=demo_graphics.pdf --export-latex');

%%
% Sometimes, we want the graphics to be smaller. There are multiple ways to
% achieve this. If we want to scale everything, including the line widths,
% the GlobalSize option can be used.
setting.svg.GlobalScale = 0.6;
set(gcf, 'UserData', setting);
plot2svg('demo_graphics_smaller.svg');
system('inkscape -z --export-area-page --file=demo_graphics_smaller.svg --export-pdf=demo_graphics_smaller.pdf --export-latex');

%%
% Call pdflatex to compile the document. Again, this only works if the
% path to pdflatex is set up.
system('pdflatex inkscape_demo.tex');
Loading