Combining information of tex and eps file generated via gnuplot to a single figure file? - pdf

I use gnuplot with epslatex option to generate figure files for plotting purposes (like here). Via this method you get 2 files corresponding to same figure, one tex file and one eps file. The figure information is in eps file and font information is in tex file. So my question is this :
Can I combine both font information and figure content to a single file like pdf / eps file ?
UPDATE : OK I forgot to mention one thing. Off course set terminal postscript eps will give me eps outputs, but it will not embed latex symbols in the plot as labels etc.

So I found a method which I got from Christoph's comment. Set terminal like set terminal epslatex 8 standalone and then finally after plotting do something like below:
set terminal epslatex color standalone
set output "file.tex"
set xrange [1:500]
set ylabel "Variance (\\AA\\textsuperscript{2})" # angstoms
set mxtics 4
plot "version1.dat" using 1:3 with linespoints pointinterval -5 pt 10 lt 1 lw 3 title 'label1' , \
"version1.dat" using 1:2 with linespoints pointinterval -5 pt 6 lt -1 lw 3 title 'label2';
unset output
# And now the important part (combine info to single file) :
set output # finish the current output file
system('latex file.tex && dvips file.dvi && ps2pdf file.ps')
system('mv file.ps file.eps')
unset terminal
reset
These steps do output tex file which is converted to dvi and ps file. And finally you rename the postscript file to eps. Now you have figure information and tex symbol information in single file. This eps file is accepted by latex files.
OK now why this works : Sorry I don't know the entire technical details. But this is working fine with me.

Related

Batch conversion of PDF to EPS via command line

I am trying to convert some figures from *.pdf to *.eps via Inkscape and Python. I resorted to the Inkscape Manual and Inkscape Wiki and came up with the code below. However when I run the script, no output is written into the working directory.
import os
import subprocess
# Change to working directory
os.chdir("C:/Users/Username/Figures")
# Iterate over all PDF figures
for figure in [i for i in os.listdir() if i[-4:]=='.pdf']:
subprocess.run([
"C:/Program Files/Inkscape/inkscape.exe",
figure, # Input figure
f"--export-filename={figure[:-4]}.eps", # Output figure
"--batch-process"
])
There is also a similar question on Stackoverflow. However, if I replace *.emf with *.eps in the answer the files cannot be read by LaTeX.
Update: Inkscape version 0.92.4 (5da689c313, 2019-01-14)

PGF / LaTeX Backend in Matplotlib via Jupyter Notebook SLURM Job on HPC System

I am a university student using my university's computing cluster.
I installed Tex Live to my home directory at ~/.local/texlive/. I have a file called mplrc. The MATPLOTLIBRC environment variable is set to the mplrc file. The mplrc file contains the following lines
backend: pgf
pgf.rcfonts: false
pgf.texsystem: pdflatex
pgf.preamble: \input{mpl_settings.tex}
text.usetex: true
font.family: serif
font.size: 12
The mpl_settings.tex file is in the same directory as the mplrc file and contains the following
\usepackage{amsmath}
\usepackage[T1]{fontenc}
\usepackage{gensymb}
\usepackage{lmodern}
\usepackage{siunitx}
On the cluster I am using, I must submit a SLURM job to run the Jupyter notebook. The example code I am trying to run within the notebook is
formula = (
r'$\displaystyle '
r'N = \int_{E_\text{min}}^{E_\text{max}} '
r'\int_0^A'
r'\int_{t_\text{min}}^{t_\text{max}} '
r'\Phi_0 \left(\frac{E}{\SI{1}{\GeV}}\right)^{\!\!-γ}'
r' \, \symup{d}A \, \symup{d}t \, \symup{d}E'
r'$'
)
def power_law_spectrum(energy, normalisation, spectral_index):
return normalisation * energy**(-spectral_index)
bin_edges = np.logspace(2, 5, 15)
bin_centers = 0.5 * (bin_edges[:-1] + bin_edges[1:])
y = power_law_spectrum(bin_centers, 1e-5, 2.5)
relative_error = np.random.normal(1, 0.2, size=len(y))
y_with_err = relative_error * y
fig, ax = plt.subplots()
ax.errorbar(
np.log10(bin_centers),
y_with_err,
xerr=[
np.log10(bin_centers) - np.log10(bin_edges[:-1]),
np.log10(bin_edges[1:]) - np.log10(bin_centers)
],
yerr=0.5 * y_with_err,
linestyle='',
)
ax.text(0.1, 0.1, formula, transform=plt.gca().transAxes)
ax.set_yscale('log')
fig.tight_layout(pad=0)
plt.show()
This generates an enormous error message, but the root of it is
RuntimeError: latex was not able to process the following string:
b'lp'
However, underneath that, I see what I think is the real problem
! LaTeX Error: File `article.cls' not found.
I've set my PATH so that it finds the right latex command, but what else needs to be set in order to find the article.cls file? It seems like it's something particular to the Python notebook. When running kpsewhich article.cls in a terminal within the Jupyterlab interface, the file gets found. But trying ! kpsewhich article.cls or subprocess.run(['kpsewhich', 'article.cls']) within the Python notebook does not find the file.
I figured it out. I forgot I had run a section of code which set
TEXINPUTS=/path/to/some/directory
Looks like I missed a : in my TEXINPUTS, so TeX was only looking in /path/to/some/directory
The solution was to have
TEXINPUTS=/path/to/some/directory:
That way it looked in my current directory, but also continued looking elsewhere.

gnuplot: Create multiple boxplots from different data files on the same output

I have a set of files that contain data that I want to produce a set of box plots for in order to compare them. I can get the data into gnuplot, but I don't know the correct format to separate each file into its own plot.
I have tried reading all the required files into a variable, which does work, however when the plot is produced, all the boxplots are on top of each other. I need to get gnuplot to index each plot along one space for each new data file.
For example, this produces the output with overlaying plots:
FILES = system("ls -1 /path/to/files/*")
plot for [data in FILES] data using (1):($4) with boxplot notitle
I know the X position is being stated explicitly there with the (1), but I'm not sure what to replace it with to get the position to move for each plot. This isn't a problem with other chart types, since they don't have the same field locating them.
You can try the following.
You can access the file in your file list by index via word(FILES,i). Check help word and help words. The code below assumes that you have some datafiles Data0*.dat in your directory. Maybe there is a smarter/shorter way to implement the xtic labels.
Code:
### boxplots from a list of files
reset session
# get a list of files (Windows)
FILES = system('dir /B "C:\Data\Data0*.dat"')
# set tics as filenames
set xtics () # remove xtics
set yrange [-2:27]
do for [i=1:words(FILES)] {
set xtics add (word(FILES,i) i) rotate by 45 right
}
plot for [i=1:words(FILES)] word(FILES,i) u (i):2 w boxplot notitle
### end of code
Result:

Gnuplot: how to use type-1 font in PDF

Gnuplot 4.6.5.
Gnuplot generate pdf files with True Type fonts by default, more specifically True Type (CID) fonts.
I have problem in processing the generated PDF file using Ghostscript because it has CID fonts. How can I use type 1 fonts in the PDF and get ride of CID fonts?
Here is a test script:
set term pdfcairo
set output "gpdf.pdf"
#set xlabel "α"; set ylabel "β"
set xlabel "x"; set ylabel "y"
plot sin(x)
When I open the PDF and its document property shows:
With the terminal's font option you must select a font which is available as Type1 on your system (and reachable for libpango). Here, on Windows, I could use "Univers LT 55", which is the only Type1 font I have installed at the moment:
set term pdfcairo font "Univers LT 55"
set output "gpdf.pdf"
set xlabel "x"; set ylabel "y"
plot sin(x)
But then you'll most probably run into problems when using utf-8 encoding. What's the actual problem you're having with ghostscript and CID fonts?

Some inline plots won't print from Ipython noteboook

I am using Ipython Notebook to generate some bar plots.
The code cell is this:
kcount =0;for k, v in pledge.groupby(['Date','Break']).sum().Amount.iteritems():
if k[0] <> kcount:
kcount=k[0]
pledge[pledge.Date==k[0]].groupby(['Break','Progcode'])['Amount'].sum().plot(kind='bar')
plt.title(k[0])
plt.figure()
This gives me a bar plot for every day of our pledge drive, showing how each show within that day did. 24 charts in all. They display great as output on the screen, but when I use the Print button in Ipython Notebook, it only prints enough graphs to fill the last page, which can vary from 3 to 6 graphs depending on the printer used. One printer used reported that it required 11x17 paper for the print job (not something I set anywhere) and when I manually set it for 8 1/2 x 11, it again only printed out the first 3 pages. I am at a loss as to what to do at this point.
As a workaround, can you can use plt.savefig('filename.png') (or .jpg, or .whatever) to save an image file and then print the files out manually?
I ended up saving these pages to a multipage PDF file and then printing them from there.
Consult the docs http://matplotlib.org/api/backend_pdf_api.html
To see how to save several figures to a multipage PDF file.
This also looks like a good resource. http://blog.marmakoide.org/?p=94