2
$\begingroup$

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}
$\endgroup$
7
  • $\begingroup$ 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 $\endgroup$
    – MarcoB
    May 11, 2015 at 20:48
  • $\begingroup$ @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? $\endgroup$ May 11, 2015 at 20:54
  • 1
    $\begingroup$ 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. $\endgroup$
    – MarcoB
    May 11, 2015 at 21:06
  • $\begingroup$ @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. $\endgroup$ May 11, 2015 at 21:09
  • $\begingroup$ 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. $\endgroup$
    – MarcoB
    May 11, 2015 at 21:11

2 Answers 2

5
$\begingroup$

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

$\endgroup$
1
  • $\begingroup$ Specifying Background->None as an option to Export worked on my system too! $\endgroup$ May 12, 2015 at 6:47
0
$\begingroup$
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

$\endgroup$
14
  • $\begingroup$ I want to make the background disappear. Simply specifying a color would be not a very good general solution if that makes sense. $\endgroup$ May 11, 2015 at 20:55
  • $\begingroup$ @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. $\endgroup$ May 11, 2015 at 20:58
  • 1
    $\begingroup$ The second PDF does not actually have a background. It's transparent. Sorry if there is any misunderstanding. $\endgroup$ May 11, 2015 at 21:02
  • 1
    $\begingroup$ @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. $\endgroup$
    – MarcoB
    May 11, 2015 at 21:08
  • 1
    $\begingroup$ You might try specifying Background->None as an option to Export $\endgroup$
    – chuy
    May 11, 2015 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.