27

I will be using Microsoft Excel to produce graphs and I was wondering if there was a simple way to put those graphs into a LaTeX document?

3
  • You have to export the graphs to eps or pdf or jpg and then include them with \includegraphics{}.
    – Sigur
    Dec 28, 2012 at 23:06
  • So I just save the graphic to .eps, .pdf or .jpg? I have heard that .png is the best file format to use? Is this right?
    – CAF
    Dec 28, 2012 at 23:13
  • @CAF: No, PNG is a raster format, which means it is not scalable. PDF or EPS are much better.
    – Siyuan Ren
    Dec 29, 2012 at 0:56

5 Answers 5

29

You can put the graph as a seperate document tab (opposed to inside a sheet). Then you can print it to PDF, using either some installed PDF printer or the Office built-in PDF printer. Then you can include the graph using the package graphicx and the command \includegraphics[width=\linewidth]{graph.pdf}.

You can as well download the TeX fonts as OTF and use them in Office, achieving the font consistency. The basic Computer Modern can be gotten here in the renewed version Latin Modern:

http://www.gust.org.pl/projects/e-foundry/latin-modern/download

9
  • 8
    Or use the data points and plot the graph using pgfplots or tikz.
    – user11232
    Dec 28, 2012 at 23:11
  • @HarishKumar TBH, If I had to do it, and I had some calculations in Excel, I would prefer exporting the graphs, and would only care about the proper font family and eventually font size.
    – yo'
    Dec 28, 2012 at 23:13
  • Where you say save to pdf, could I use png? I have heard this is the most reliable format. If so, why?
    – CAF
    Dec 28, 2012 at 23:20
  • 3
    In this case, pdf is certainly better, because png is raster graphics, making it badly scalable. For this, you are safe with pdf because you don't use any special functions (hyperlinks, embedded videos or who-knows-what).
    – yo'
    Dec 28, 2012 at 23:24
  • I have tried this code, but the following error message appears: 'The required file tex\context\base\supp-pdf.mkii is missing. It is part of the following package: mptopdf. The package will be installed from: (link to choose repository directory). It then allows an option to install, but I have clicked this and the exact same message appears again and again... Any ideas?
    – CAF
    Dec 29, 2012 at 9:02
19

In addition to what the other posters said it is worth mentioning that one can also use VBA code to create regular exports from Excel, quite helpful if you need to repeat the exports because the data have changed.

A while ago I wrote a blogpost on this, the following VBA code is taken from this post:

Sub ExportAllCharts()

    If ActiveSheet.ChartObjects.Count > 0 Then
        For Each Diagram In ActiveSheet.ChartObjects
            ActiveSheet.ChartObjects(Diagram.Name).Activate
            Filename = ActiveChart.Name
            ActiveChart.Axes(xlValue).MajorGridlines.Select
            ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "C:\" & Filename, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
        Next Diagram
    End If

End Sub
6
  • Is there something like this for libre office?
    – dani
    Jul 25, 2015 at 12:12
  • I am not aware of it, but I can imagine... Jul 25, 2015 at 13:36
  • @UweZiegenhagen unfortunately I just get an 1004 error, despite the variable seem to be initialized correctly the last line, before Next seems to break. :(
    – JTIM
    Jun 20, 2017 at 6:56
  • Oh forgot to add it was in Excel 2013
    – JTIM
    Jun 20, 2017 at 8:03
  • 1
    Maybe adjust the path, "C:\" could be not writable. If that won't help I don't know, as I can't look at your code. Jun 21, 2017 at 5:08
2

I wanted to add a very simple but effective way of how I was doing it. I was using Excel to graph results, because I didn't have that many graphs (much more tables). Nontheless I think the outcome is quite good and sufficient for many cases (exceptions would be if you needed to create a lot of graphs, then you might prefer other programs instead, like mathlab, or Latex packages to achieve a more consistent look with regard to captions).

First mark the graph/diagram and use file/save as...Here you must choose pdf-format. I would save the file in the same folder where you usually save all your tex-files.

The next step is to crop the image. There are numerous ways, e.g. Photoshop, IrfanView. A more professional, automated way would be pdfcrop. You need to download Perl first and install, then you can use it. A little warning: You need to use the command line for pdfcrop. The advantage is that you can very quickly crop all your images perfectly with little to no margins. There are many tutorials/explanations on these programs to find.

The final steps have been described before. You need to load the package graphicx and the command to insert a graph_x is \includegraphics[width=\linewidth]{graph_x.pdf}.

That's it :)

2

This is another solution if you have Visio. This is the explanation for Visio 2010.

Step 1. Copy and paste the graph. Copy/paste your Excel graph to Visio. Be sure that it is pasted as Microsoft Office Excel Worksheet (this should be the default, but it can be ensured using the Paste Special option instead of Paste).

Step 2. Remove blanks spaces around the graph. Go to Design tab, then find Page Setup, go to Size and choose Fit to Drawing. This removes everything but margins. To remove margins, open the Page Setup (click on the small icon in the bottom right corner of this ribbon area), go to Print Setup tab, click on Setup and set the margins (it seems that the left margin cannot be set to 0, at least not using this approach). Then click OK twice.

Step 3. Create the PDF file. Now go to File, then Save As, from the Save as type list choose PDF. Click Options, adjust the options, then click OK and finally Save. Now there should be the PDF file with the graph.

Step 4. Import this PDF file into a LaTeX document. For this, the \includegraphics command should be used.

1
  • 1
    Update. Similar can be achieved using Word. Just paste an Excel graph to Word as a graph (not a picture or a metafile). Then set margins to 0, change page size and save as PDF.
    – MMSt
    Dec 30, 2014 at 12:05
1

If you can switch to some other spreadsheet software, you can try one of those that support export to LaTeX: https://en.wikipedia.org/wiki/Comparison_of_spreadsheet_software

1
  • 1
    Does any of those support graphics? I would have assumed they only supported tabular material. Nov 11, 2013 at 14:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .