Document not found! Please try again

Importing Professional Graphics in LATEX

0 downloads 0 Views 365KB Size Report
Whereas EPS and PDF are common examples of vector-based graphics. As in this file ... Legend Resize NEW(1.4): this line decreases the LBW and also the le-.
Institut f¨ ur Elektrotechnik

Prepared by: Tareq Abuaisha

Importing Professional Graphics in LATEX In high quality scientific papers writers try always to deal with vector-based graphics and to avoid dealing with pixel-based graphics as much as possible. Unlike pixel-based graphics which are made of pixels, vector-based graphics are made up of lines and mathematical statements which makes it easy to scale images up and down without losing its quality. Jpg, BMP, TIFF, JPEG, PNG and BITMAP are common examples of pixel-based graphics. Whereas EPS and PDF are common examples of vector-based graphics. As in this file we are speaking about high quality figures, we are going to deal only with vector-based graphics (e.g. EPS and PDF). 1. How to export a professional (high quality and well organized) eps figure from Matlab to include it in LATEX To be able to achieve this, three separate functions in three different m-files are needed: (a) Legend Resize NEW Unfavourable behaviour in dealing with legends in Matlab is that its minimum Legend-Box-Width (LBW) is fixed. This means that if the legend is quite short (e.g. P[pu]) the LBW will not shrink so that the legend contents fit rightly inside it. To solve this problem Legend Resize NEW function is used. I built this function from several related m-files to include all relevant properties. This function takes one parameter only (DecreaseLegendBoxWidthFactor), which is the factor by which the LBW is going decrease/increase. By the way, the function controls both the LBW and also readjusts the linest (marker inside the legend) thereupon. If this factor is larger than one, the LBW and thus the legend marker decreases and vice-versa. Example: Legend Resize NEW(1.4): this line decreases the LBW and also the legend marker by factor of 40%. Figure 1 shows the effect of using the function Legend Resize NEW to resize the LBW and the legend marker. The factor used in this case is 1.4. The effect of using the function is totally clear in the right hand side sub-figure of figure 1.

1

without ”Legend Resize NEW”

with ”Legend Resize NEW”

1

1 V(X) 0.8

0.6

0.6

0.4

0.4

0.2

0.2

sin(2 ∗ π ∗ t)

sin(2 ∗ π ∗ t)

V(X) 0.8

0

−0.2

0

−0.2

−0.4

−0.4

−0.6

−0.6

−0.8

−0.8

−1 −2

−1

0 time [sec]

1

−1 −2

2

−1

0 time [sec]

1

2

Abbildung 1: Effect of using the function Legend Resize NEW to resize the LBW with the legend marker

(b) format ticks It is also rather clear from figure 1 that eps can provide a high quality when used in LATEX especially when zooming into the figure. Try to see the effect of zooming in (e.g. zoom in the page to 800%). From figure 1 it can be realized that a LATEX text-interpreter was used for x and y labels and for the titles as well, and thus these texts are also consistent with the text in the rest of the document (same text type i.e. Computer Modern Text Font). The question which does emerge is that How can I force my x and y ticks to have the same font type (a LATEX text-interpreter)? Unfortunately Matlab doesn’t offer this property, at least for the mean time. Nevertheless, to solve this problem Alex Hayes wrote the abovementioned function. I needed to be able to change independently x and y offsets (the offset is the distance between the ticks and the axis) along with some other properties, and thus I modified the above-mentioned function to become format ticks NEW. This function takes the handle of the current axis as input (i.e. gca). The effect of using this function is obvious in figure 2. All the text in this figure including labels, titles, legends and ticks has a LATEX textinterpreter, which means that all the text included in the figure will be totally consistent with the text in the rest of the document at any text type. Is there still anything which can be performed on figure 2 to be ready for professional publications? The answer to this question is YES there is still a room for improvement on figure 2 by using the function spaceplots.

2

without ”Legend Resize NEW”

with ”Legend Resize NEW”

1

1 V(X)

V(X) 0.5 sin(2 ∗ π ∗ t)

sin(2 ∗ π ∗ t)

0.5

0

-0.5

0

-0.5

-1 -2

-1

0 time [sec]

1

-1 -2

2

-1

0 time [sec]

1

2

Abbildung 2: Effect of using the function format ticks NEW to give x and y ticks a LATEX text-interpreter (c) spaceplots This function was written by Aditya Joshi at 2012. It is used to perform a reposition of the plots/subplots in a figure by applying a custom spacing between them. Example: spaceplots([left right top bottom],[horizontal vertical]): this applies the spacing between the subplots within the figure on one side, and the spacing between the figure and its borders on the other side. This process is shown in figure 3. Top

subplot (a)

subplot (b)

Right

Left Horizontal

Vertical subplot (c)

subplot (d)

Bottom

Abbildung 3: The function spaceplots applies the required spacing

3

Note that in case of a single plot within the figure so the command of using the function spaceplots becomes spaceplots([left right top bottom]), since their is no horizontal and vertical distances. The effect of applying the function spaceplots on figure 2 is shown in figure 4. with ”Legend Resize NEW”

without ”Legend Resize NEW” 1

1 V(X)

V(X)

0.5 sin(2 ∗ π ∗ t)

sin(2 ∗ π ∗ t)

0.5

0

-0.5

0

-0.5

-1 -2

-1

0 time [sec]

1

-1 -2

2

-1

0 time [sec]

1

2

Abbildung 4: Effect of using the function spaceplots to perform the required spacing

Actually the effect of applying the function spaceplots is not totally obvious due to the reason that the gray spacing between the two subplots and also between the figure and its borders are converted into white color since we are using the Matlab order set(gcf,’inverthardcopy’,’on’) to invert the gray into white. But what is obvious is that the width of figure 4 is larger than figure 2 even they were both drawn using the LATEX command includegraphics[width= \linewidth], but this is due to the reason that the white gaps between the figure and its borders are now being utilized as increased figure size. Figure 4 is now finalized and ready to be used in professional publications. (d) How to save the figure into eps in Matlab A possible way is to save the figure directly file ==> save as ==> save as type .eps. But due to the fact that in this method the changes applied by the function Legend Resize NEW might be lost, this method is not considered as a professional figure saving. As a replacement we are going to use these three Matlab orders: set(gcf,’inverthardcopy’,’on’); % inverts the gray gaps into white set(gcf,’PaperPositionMode’,’auto’); % makes the dimension of the printed figure equals to the dimension of the figure at the screen print(’-dpsc2’,’with without 3.eps’) % saves the figure as eps in the Matlab current folder.

4

(e) General Notes • It is sometimes needed to move the title, legend or even x and y labels of the figure before printing it into eps; this can be done from tools ==> Edit Plot. • If the offsets of x and y axis (the offset is the distance between the ticks and the axis) need to be changed; then open the m-file of the function format ticks NEW and change them as required. • The Matlab command set(0,’DefaultTextInterpreter’, ’latex’) has to be used before starting the plotting in order to force all the labels, titles, legends and ticks to have a LATEX text-interpreter. • The function spaceplots simply repositions the axes in a figure, it does not change any other properties of the figure and it must be called lastly after the plotting is done. • Since you are going to save figures into eps regularly, it is a good idea to make m-file called e.g. Print Graph which includes the abovedescribed three commands. Don’t forget to change the name of the saved file every time to avoid overwriting the old files. • To force all the figures within the same publication to have the same size and properties, be sure to write these commands in your m-file before starting the drawing: %===================================== set(0,’Units’,’pixels’);scnsize = get(0,’ScreenSize’); fig1 = figure; set(0,’DefaultTextInterpreter’, ’latex’) set(gcf,’inverthardcopy’,’off ’); set(gcf,’paperpositionmode’,’auto’); pos1 = [500,200,800,500]; set(fig1,’OuterPosition’,pos1); %===================================== • The command set(fig1,’OuterPosition’,[left bottom width height]); is used to set the figure in a specific position on the monitor and to give it a specific size as well. Thus all the figures within the same scientific publication are going to have the same size when drawn by the same method. My monitor size is 1920*1080 pixels. And my figure size is 800*500 pixels (see the above Matlab command).

5

Figure 5 explains the function of this command.

Monitor set(fig1,’OuterPosition’,[left bottom width height]) Figure 1

Height

Width

Bottom

Left

Abbildung 5: Set the figure in a specific position and give it a specific size All the used Matlab m-files, LATEX files and also the included figures are to be attached with this pdf file. 2. How to export a professional eps/pdf figure from Matlab Simulink model to include it in LATEX (a) Export an eps figure from Matlab Simulink model The following Matlab command is used: print -depsc -zbuffer -r500 -sSimulinkModelName.eps where depsc¨ıs used to print colored eps graphic format, and zbuffer¨ıs used to insure that the printed output matches what you see on the screen, and r500ßets the output resolution to 500 dots per inch. Don’t forget the s before the simulink model name. If you want to print a subsystem inside the main system then the command changes to: print -depsc -zbuffer -r500 -sMainSimulinkModelName/SubsystemName.eps Figure 6 shows a Matlab Simulink model which was imported into LATEX using this method.

Wind Farm (1.5 MW *4) = 6 MW



A

Set−up System A N

B C

120 kV

A a B b C c 2500 MVA X0/X1=3

Source is programmed to make voltage dip from 0.3 − 0.5 sec

A

a

B

b

C

c

120 kV/25 kV 47 MVA

A B C

aA bB

a b c

WF Total

B

cC

25 km line

B 25kV (25 kV)

WTs

C

Wind_Farm Discrete, Ts = Sample_Time s. Individuals powergui

Abbildung 6: Importing a Matlab Simulink model using the method 2a

6

(b) Export a pdf figure from Matlab Simulink model In this method the user must have an Adobe Acrobat Professional Pro or Adobe Acrobat XI Pro. It is done in two separate steps as follows: The first step: − From file menu choose file ==> Print... − Choose the Adobe PDF printer, then go to Properties. − From Properties go to Layout tab to choose whether you prefer Portrait or Landscape format; the preference should be based on the ratio between the height and width of your model. − Leave the layout and go to Paper/Quality to choose whether you need colored or black & white printing. − Stay in the Paper/Quality tab and go to Advanced... − When needed you can change the Paper Size, but the most important is to choose the Print Quality. Use quality 600 dpi or higher (preferred value 1200 dpi.) − Press two times OK to leave first the Advanced properties and then to leave the whole Properties. Finally press OK to print the figure and then save it where desired. The second step: − Before importing the figure in LATEX it is necessary first to crop or extract the figure only from the complete pdf page. Otherwise the figure will occupy relatively large area when being imported into LATEX without achieving the desired quality. − To be able to perform this step, the user should have an advanced Adobe Acrobat reader software (e.g. one of the two software mentioned above in part 2b). − Open the printed pdf file from the first step. From View menu choose Tools and then from the available tools choose Pages. − From Pages choose Crop. Before cropping/extracting the figure, it is desirable first to zoom into the page (e.g. to 800%) then start extracting the figure. Note that the extracting is done by firstly choosing only the area occupied by the figure, and then double clicking this area by the left mouse key. A Set Pages Boxes menu pops up, if everything looks fine press OK. − The new pdf page should have a small area which only includes the figure where the rest is cropped out. Finally save the new pdf file by the required name (e.g. fig5-1). − The figure is ready now to be included in LATEX using the command: \includegraphics[width=\linewidth]{fig5-1.pdf}

Figure 7 shows the same Matlab Simulink model drawn in figure 6 but this time using the second method.

7



Wind Farm (1.5 MW *4) = 6 MW



Set-up System N 120 kV

A WF

A

A

a

A

a

A

a

B

B

b

B

b

B

b

C

C

C

c

C

c 2500 MVA X0/X1=3

120 kV/25 kV 47 MVA

aA bB

Total B

cC

c 25 km line

B 25kV (25 kV)

WTs

C



Source is programmed to make voltage dip from 0.3 - 0.5 sec

Wind_Farm Discrete, Ts = Sample_Time s. Individuals powergui

Abbildung 7: Importing a Matlab Simulink model using the method 2b By comparing the figures 6 and 7, it can be realized that when applied correctly the two methods offer almost the same quality. There is only a relatively small difference in the fonts, this is due to the reason that sometimes eps printing doesn’t have the same font type as in the figure. In this case the font is being replaced with a comparable one. General Notes: • If you couldn’t find the cropping tool where it is described above, then be sure which version of Adobe Acrobat reader you have and then search in internet how to make cropping in that version. • This method (2b) is somehow comparable to the method used internally in LATEX to draw eps figures. LATEX uses the package epstopdf to firstly convert the eps figure into a pdf one, then it plots the figure based on its pdf version. This is the reason why you will always find a pdf file with the name epsFileName-eps-converted-to after compiling an eps figure with the name epsFileName. • If your LATEX file includes many eps figures, then the time required by the first compilation is relatively larger than the time required by the preceding compilations. This is due to the reason that the creation of the pdf file is done whenever the file epsFileName-eps-converted-to doesn’t exist in the current directory, which is not the case in the preceding compilations. • This method is done not only for Matlab Simulink models, but whenever you can’t export the eps figure (e.g any simulation software which doesn’t support eps or even for figures in other pdf files; as long as the writers rights are not violated).

8

3. How to draw vector-based graphics using Inkscape and then include them in LATEX Inkscape is an open source vector-based graphics editor. Its goal is to implement full support for Scalable Vector Graphics (SVG). Since we are interested in vector-based graphics mainly for the advantages which they can offer, during this part of the document Inkscape is adopted for further explanation. Besides its biggest advantage as being open source software, Inkscape is user friendly and has many capabilities especially when it is linked with LATEX through the famous and extremely powerful TikZ package. (a) Export an eps figure from Inkscape directly without using the TikZ package It is assumed here that the reader has a basic knowledge of Inkscape, nevertheless as already mentioned Inkscape is a user friendly software and it is quite fun to learn it. This link offers good video tutorials. Figure 8 was exported directly as eps from Inkscape and then imported in LATEX using the command \includegraphics[scale = 0.5]{fig8.eps}. Top

subplot (a)

subplot (b)

Right

Left Horizontal

Vertical subplot (d)

subplot (c)

Bottom

Abbildung 8: Exporting a figure directly from Inkscape (file ==> save as ==> eps) (b) Export a TikZ graphics from Inkscape To be able to do this, at first you have to download three files (tikz export.py & tikz export effect.inx & tikz export output.inx ) from this link. Then copy and past these files in the directory ...\Inkscape\share\extensions and then restart the Inkscape if it was open. Now you will be able to see the option TikZ code (*.tex) within the Save as type menu. Figures 3 and 5 were firstly exported from Inkscape as TikZ code and then the code was modified to draw the arrows and to write the texts. Comparing figure 3 (exported using the method 3b) with figure 8 (exported using the method 3a), it can be realized that both figures do have quite high quality but figure 3 has the advantage of having consistent font type with the rest of the document with little higher quality as well. Actually figures 3 and 5 have the ultimate quality which can ever be achieved since they were typeset internally in LATEX exactly as the text in the document. However drawing a figure internally using LATEX requires usually a great 9

effort and a good knowledge of the TikZ package as well. The wonderful manual of TikZ package was written in 2007 by Till Tantau and can be downloaded from this link. A minimal manual can be found here and a short tutorial can be found here as well. Generally speaking TikZ package is the most powerful LATEX tool, there are no limits for its capability. It is capable of drawing any figure which you can imagine. 4. Finalizing ... − Inkscape can be used to trace any free-style drawn graphic (e.g. jpeg or bitmap) by using Path ==> Trace Bitmap and then by using Path ==> Break Apart you can break the resulted figure into different parts. For more explanation see these two videos. − It is always advisable to have a separate folder (e.g. figures) for figures and then to include them using the LATEX command: \includegraphics[width=\linewidth]{figures/fig8.eps}. This will help you a lot to organize your files. − I found out that by using Inkscape we can write a LATEX interpreted text and LATEX formulas as well. This definitely increases the quality of eps figures which are being exported directly from Inkscape using the method 3a. For more information check the following link. I tried to follow the steps in the above link, but I got only a partial success. That I could have the option LATEX formula under the path: Extensions\Render\LATEX formula, there I could add mathematical formulas and it appears correctly but I get the error that the file: \pstoedit\drvmagick.dll doesn’t exit (Info: I am using Inkscape 0.48). So if could have better results, please do inform me. − This document1 summarizes my little experience in dealing with graphics in Matlab, LATEX and Inkscape during the work of my master thesis. I am presenting it to you as a very precious gift, because I believe that in order to improve our knowledge we have first to spread it. Let’s all make this our motto!

With my best wishes and regards ^ ¨ M.Sc. Tareq Abuaisha

1

This document was typeset using LATEX by the author. Any changes, modifications or citations are to be made after to his acceptance M.Sc. Tareq Abuaisha, TU Bergakademie Freiberg, Institut f¨ ur Elektrotechnik Email: [email protected] Together to make LATEX the ultimate typesetting environment ...

10