2

Update: WRI confirmed that this is a known issue with Export on 2015-05-14.

I am trying to export the outputs of some Plot expressions as PDF files for inclusion in a $\LaTeX$ document.

The two graphs are produced using the following code:

Plot[{
  PDF[NormalDistribution[], x],
  PDF[NormalDistribution[-4, 1], x],
  PDF[NormalDistribution[2, 1], x]
  },
 {x, -8, 6},
 AxesLabel -> {None, None},
 Ticks -> {Automatic, None},
 Axes -> {True, False}
 ]
Export["NormalDistMean.pdf", %]
Plot[{
  PDF[NormalDistribution[], x],
  PDF[NormalDistribution[-4, 1], x],
  PDF[NormalDistribution[2, 1], x]
  },
 {x, -8, 6},
 AxesLabel -> {None, None},
 Ticks -> {Automatic, None},
 Axes -> {True, False},
 PlotLegends -> {"N(0,1)", "N(-4,1)", "N(2,1)"}
 ]
Export["NormalDistMeanAns.pdf", %]

Then I included them in a $\LaTeX$ document (Beamer). The complete documents are shown below for the first PDF (no legends) and second PDF (with legends), respectively:

no legends

with legends

The first graph shows up in the $\LaTeX$ document with a white background, whereas the second one doesn't, i.e. the plot was transparent, as was the original intended outcome for both.

Here is the question: How do I make the first PDF like the second one?

Thanks!


The $\LaTeX$ (beamer) code is included below for those who are interested.

\documentclass{beamer}
\usepackage{color,graphicx}
\mode<presentation>{
    \usetheme{Darmstadt} %Best
    \setbeamercovered{transparent}
}
\beamertemplatenavigationsymbolsempty % navigation bars

\begin{document}

\begin{frame}{Mean}
  \centering
  \only<1>{
   \begin{block}{Can you find the $\mu$ for each of the $N(\mu,1^2)$ density curves?}
    \includegraphics[height=0.6\textheight]{NormalDistMean.pdf}
   \end{block}
  }
  \only<2>{
   \begin{block}{Can you find the $\mu$ for each of the $N(\mu,1^2)$ density curves?}
    \includegraphics[height=0.6\textheight]{NormalDistMeanAns.pdf}
   \end{block}
  }
 \end{frame}

\end{document}
MarcoB
  • 67,153
  • 18
  • 91
  • 189
Chen Stats Yu
  • 4,986
  • 2
  • 24
  • 50
  • Have you tried adding Background -> None as an option to your plots? This seems to work on MMA v.10.1.0 on Win7-64bit – MarcoB May 11 '15 at 20:48
  • @MarcoB I am using the same MMA V10.1.0 on Win7 X64. But it did not seem to work. How did you know whether it worked? Did you compile it in latex as well? As far as I can tell, PDF reader won't tell the difference since both background are white? – Chen Stats Yu May 11 '15 at 20:54
  • 1
    I didn't compile it into latex, but I imported the PDF files into a vector graphics program (InkScape: you should check it out, it's free / open source, and it's great). I then ungrouped the imported PDF vector graphics. The first PDF without the Background->None option had a white background panel; when I used Background->None, the white background was not there anymore, so effectively the plot was transparent. – MarcoB May 11 '15 at 21:06
  • @MarcoB Hmmmm, that's odd! Yes, I know about InkScape. Thanks! I have Acrobat, so I can verify this myself too. It does not work on my system. I have also tried to latex it. The problem still remains. – Chen Stats Yu May 11 '15 at 21:09
  • Have you tried exporting to EPS rather than PDF, to see if it makes any difference? I'm not familiar with latex, so I don't know if that would work just as well to produce your final document. – MarcoB May 11 '15 at 21:11
  • @MarcoB Yes, any post-script (.PS, .PDF, .EPS) will work fine with latex. I personally tend to use PDF since it is more common for people to open with any (free) PDF reader. Just for the sake of my current, I can use EPS, it works. – Chen Stats Yu May 11 '15 at 21:38
  • I delved a bit deeper in the issue, and I can't explain this behavior of Export. It looks like a bug to me. I've posted an answer containing a description of what I've tried, and a viable workaround first suggested by @chui in a comment. – MarcoB May 12 '15 at 03:44

2 Answers2

5

Update 2015-05-14

I contacted WRI support and they confirmed that this is a known issue with Export (support case: 3206586).

Summary

@ChenStatsYu seems to have found an unexplained behavior of the Export function for PDF files that looks like a bug.

Detailed results

1) I generated two graphics similar to those in the OP's original question, then exported them to PDF using Export. I am including the Background -> None option to Plot for completeness, but this is already the default for Plot, as one can check by running Options[Plot]. I have also separately tried without that explicit option. It does not make a difference to the discussion below.

Plot[
 {Exp[-(x - 7)^2/4], Exp[-(x - 5)^2/3]},
 {x, 0, 15},
 AxesLabel -> None,
 Ticks -> {Automatic, None},
 Axes -> {True, False},
 Background -> None
]
Export["nolegend.PDF", %]

Plot[
 {Exp[-(x - 7)^2/4], Exp[-(x - 5)^2/3]},
 {x, 0, 15},
 AxesLabel -> None,
 Ticks -> {Automatic, None},
 Axes -> {True, False},
 PlotLegends -> "Expressions",
 Background -> None
]
Export["withlegends.PDF", %]

2) I opened those two PDF files in a vector graphics program, and verified that the one generated from the graph without legends included a white opaque panel placed behind the graphs, effectively as a white background. This is the same construct that one would find when exporting the output of Plot containing a colored background.

3) Even though it was generated from completely equivalent expressions, the PDF generated from the legended graph DID NOT CONTAIN that white panel, effectively making the image transparent, as both should have been.

4) I reimported the PDF files back into Mathematica. Here are two two images attempting to summarize the relevant parts of those two files:

Grid[
  ArrayReshape[#, {2, 4}]& @( Graphics /@ Import["nolegend.PDF"][[1, 1]] ),
  Frame -> All
]
Grid[
  ArrayReshape[#, {2, 4}]& @( Graphics /@ Import["withlegends.PDF"][[1, 1]] ), 
  Frame -> All
]

WITHOUT LEGENDS structure of PDF without legends

WITH LEGENDS structure of PDF with legends

The second and third frames from the left in the top row of the structure of the imported PDF without legends are two white opaque panels of the same size as the curves. These panels are not there in the PDF with legends.

chui's suggestion

@chui suggested in a comment to include Background -> None as an option to Export, rather than to Plot. He is onto something! Indeed, that fixed the exported PDF generated from the plot without legends. The two extra white panels where not generated this time! I generated the following "table of contents" from this latter PDF:

exported PDF no legends, option in Export

MarcoB
  • 67,153
  • 18
  • 91
  • 189
0
Plot[{PDF[NormalDistribution[], x], PDF[NormalDistribution[-4, 1], x],
   PDF[NormalDistribution[2, 1], x]}, {x, -8, 6}, 
 AxesLabel -> {None, None}, Ticks -> {Automatic, None}, 
 Axes -> {True, False},
 Background -> None,
 PlotLegends -> Placed[{"N(0,1)", "N(-4,1)", "N(2,1)"}, {1, .5}]]

enter image description here

David G. Stork
  • 41,180
  • 3
  • 34
  • 96
  • I want to make the background disappear. Simply specifying a color would be not a very good general solution if that makes sense. – Chen Stats Yu May 11 '15 at 20:55
  • @ChenStatsYu Your explicit question was "How do I make the first PDF like the second one?" This problem is solved. Your subsequent question about doing this without specifying the background seems impossible, since the background default is white (unless you use SetOptions[Plot, Background -> LightBlue]. Your request to "make the background disappear" does not make sense. – David G. Stork May 11 '15 at 20:58
  • 1
    The second PDF does not actually have a background. It's transparent. Sorry if there is any misunderstanding. – Chen Stats Yu May 11 '15 at 21:02
  • Again, I can't find the technical term for this. But the second PDF does not have a background color if you like. It only has the curves and axises and texts. It will show ANY color if I put it onto a different background. – Chen Stats Yu May 11 '15 at 21:03
  • 1
    @ChenStatsYu I think you are expressing your request correctly: you want these PDF files to be exported without a background, or equivalently with a transparent background. – MarcoB May 11 '15 at 21:08
  • Background->None? – David G. Stork May 11 '15 at 21:11
  • @MarcoB There was a mis-spelling before anyone can see it! :) – Chen Stats Yu May 11 '15 at 21:11
  • @DavidG.Stork Yes, I think Background -> None should work, but @ChenStatsYu reported in our comment conversation above that it may not have worked for him. – MarcoB May 11 '15 at 21:13
  • Yes, I did not work on my system. Really odd! Windows 7 SP1 X64 with MMA V10.1.0, both English version. – Chen Stats Yu May 11 '15 at 21:15
  • @ChenStatsYu I just proved Background->None worked for me in version 10.1. So if it works for you, I'd appreciate a check "acceptance" of my solution. – David G. Stork May 11 '15 at 21:17
  • I will leave it on for the moment, so that others might suggest an alternate. But I will start a new post regarding my PDF situation. Because it is not working, for any reason. – Chen Stats Yu May 11 '15 at 21:35
  • @DavidG.Stork The one with PlotLegends -> (second one) will work fine without the Background->None. Are you getting the same for the first graph as well? Thanks. – Chen Stats Yu May 11 '15 at 21:42
  • 1
    You might try specifying Background->None as an option to Export – chuy May 11 '15 at 21:51
  • @chuy That was an intriguing suggestion, and in fact it did fix the behavior! I looked in the documentation, however, and couldn't really find a reference for the usage you proposed. Had you come across this behavior before? I can't explain this behavior of Export, and it looks like a bug to me. If you have time, could you take a look at the description of my findings that I posted as an answer to this question? – MarcoB May 12 '15 at 03:42