How to plot in Latex with Gnuplot? - latex

Earlier I have used Gnuplot and created a pdf-file which I then implemented via includegraphics in my Latex-document.
Isn't there a better way to plot directly from the Latex-document with Gnuplot?

Basically u can just follow this Youtube-Tutorial
So here are the Steps:
Step 1: Set environment variables
After having installed MikTex, TexStudio and Gnuplot click on Start -> Rightclick on Computer -> Settings -> Advanced Systemsettings -> Environment variables
Here you have to create 3 new variables:
► PATH
C:\Program Files (x86)\MiKTeX 2.9\miktex\bin;C:\Program Files (x86)\gnuplot\bin
► gnuplot
C:\Program Files (x86)\gnuplot\bin
► GNUPLOT_LIB
C:\Program Files (x86)\gnuplot\demo
Step 2: Edit TexStudio
Go to TexStudio and click on Options -> Configure TexStudio -> Commands
here you need to update the upper both commands
► LaTeX
"C:/Program Files (x86)/MiKTeX 2.9/miktex/bin/latex.exe" -src -interaction=nonstopmode %.tex
► PdfLaTeX
pdflatex.exe -synctex=1 -shell-escape -enable-write18 -interaction=nonstopmode %.tex
Step 3: Code with Gnuplot in Latex
\documentclass[a4paper]{scrartcl}
\usepackage[miktex]{gnuplottex}
\begin{document}
\begin{figure}[H]
\centering
\begin{gnuplot}[terminal=pdf]
set terminal pdf enhanced size 14.5cm, 4cm
set key top left
set xlabel 'x [1]'
set ylabel 'y [1]'
f1(x)=sin(x**2)
plot f1(x) title 'y_1 = sin(x^2)'
\end{gnuplot}
\caption{Plot}
\end{figure}
\end{document}

Related

Compile two versions of a document from the same latex source

How to automatically compile two versions of the same document, for example version without answers for the students and another version with answers for myself?
I have a small bash script to do a dual format.
function latex-ans () {
n=$(basename $1 .tex) # strip .tex in filename foo.tex -> foo
ln -s $n.tex $n-ans.tex # create a soft link (for instance foo-ans.tex -> foo.tex)
pdflatex '\def\withanswer{1} \input{'$n-ans'}' && pdflatex $n
% first format the version with answers and then the normal version
rm $n-ans.tex $n-ans.log
% remove useless files
}
If I have a file foo.tex, this commands formats both versions of the file and generates two pdf: foo.pdf and foo-ans.pdf. Thanks to the renaming of foo.tex through the ln -s, it also keeps separate foo.aux and foo-ans.aux to preserve useful information on both versions.
At the latex level, I basically do the same and use the macro \withanswers to configure my packages.
There are several packages that allow to conditionally exclude certain parts of the document, for example the exercise package.
With TeXstudio, the following magic comment can be used to automatically compile both versions at once (including repeated compilation for cross-references, bibliographies, indices etc.):
% !TeX program = latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=% -pretex="\newcommand{\version}{noanswer}" -usepretex % | latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=%_solution -pretex="\newcommand{\version}{}" -usepretex % | txs:///view-pdf "?am)_solution.pdf"
\documentclass{article}
% setting a default value in case it is compiled without the magic comment
\ifdefined\version
\else
\def\version{noanswer}
\fi
\usepackage[\version]{exercise}
\begin{document}
\begin{Exercise}[title={Title},label=ex1]
question text
\end{Exercise}
\begin{Answer}[ref={ex1}]
solution
\end{Answer}
\end{document}

Setting otherlangs from the command line using pandoc

Using pandoc, I managed to produce the following output using the YAML prolog.
---
lang: fr
otherlangs: [en]
---
Generates this latex code.
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[shorthands=off,english,main=french]{babel}
\else
\usepackage{polyglossia}
\setmainlanguage[]{french}
\setotherlanguage[]{english}
\fi
But, I cannot manage to have it working using the commande-line arguments. This what I tried.
$ pandoc -s -t latex -V lang=fr -V otherlangs="[en]"
Which produces:
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[shorthands=off,main=french]{babel}
\else
\usepackage{polyglossia}
\setmainlanguage[]{french}
\fi
Any hints on how I should pass de otherlangs variable from the command line?
AFAIK this is not possible since the -M option (and probably -V as well) parse values
as YAML boolean or string values
and otherlangs needs to be a list. What's your use case?
You shouldn't have to set otherlangs manually as it's extracted from spans and divs with the lang attribute (see the MANUAL), for example:
my [english words]{lang=en} inside a span
::: {lang=en}
followed by a native div (using this syntax since pandoc 2.0)
with another paragraph
:::
If you absolutely must set this info from the command-line, using a custom LaTeX template that contains, say, the following (if you're using Polyglossia/XeLaTeX):
\setotherlanguage[]{$myOtherLangs$}
and calling it with pandoc -V myOtherLangs="english,french" should solve your use-case.

How to make sphinx document via pdflatex \raggedright?

How can I render reST document with \raggedright command applied to all the text if I use pdflatex?
Use a custom template.
In the following example, I'll be using xelatex. I would copy the default template for xelatex from /usr/local/lib/python2.7/site-packages/docutils/writers/latex2e/xelatex.tex to a file called mytemplate.tex
The default template looks like this:
$head_prefix% generated by Docutils <http://docutils.sourceforge.net/>
% rubber: set program xelatex
\usepackage{fixltx2e}
\usepackage{fontspec}
% \defaultfontfeatures{Scale=MatchLowercase}
$requirements
%%% Custom LaTeX preamble
$latex_preamble
%%% User specified packages and stylesheets
$stylesheet
%%% Fallback definitions for Docutils-specific commands
$fallbacks$pdfsetup
$titledata
%%% Body
\begin{document}
$body_pre_docinfo$docinfo$dedication$abstract$body
\end{document}
You could add \raggedright just after \begin{document}.
For rendering the document, I'd use the following commands:
rst2xetex --template=mytemplate.tex --use-latex-docinfo mydoc.rst
xelatex mydoc.tex
xelatex mydoc.tex
rm -f *.out *.aux *.log mydoc.tex
(This is on a UNIX-like operating system.)

Run LaTeX command in Sublime 3

I'm working with LaTeX files in Sublime 3 (3059). Given a main .tex file and a .bib file holding the citations, I'd like to be able to issue the standard:
pdflatex main.tex && bibtex main.aux && pdflatex main.tex && pdflatex main.tex
command after hitting a given shortcut in Sublime.
I know there are tools that can do this (LatexTools, LaTeXing) but since what I need is (I believe) so simple, I'd prefer not to depend on extra packages.
How can I store this command and have Sublime run it following a given key combination?
Edit: Based on the answer given, this is the full command I ended up using:
{
"cmd": ["pdflatex $file_name && bibtex $file_base_name.aux && pdflatex $file_name && pdflatex $file_name"],
"shell": true
}
If you want to execute a command you could use Tools > Build System > New Build System to create a file with a content similar to this one:
{
"cmd": ["command"]
}
And then, you should select that as your project Build System. Finally, use ctrl+b to invoke build (invoke your command or script).

Synchronise pdf to Rnw in Knitr with texshop

Would anyone know how to get synctex to work from the pdf to the Rnw in knitr with texshop? It does work from Rnw to pdf. Many thanks.
This is how I worked this out. Not tried on multiple .Rnw files.
In TeXShop Preferences, make sure your "Sync Method" is set as "SyncTeX (TeX ≥ 2010)".
On your Mac, make the directory "~/Library/TeXShop/Rscripts" and put the R file "patchKnitrSynctex.R" downloaded from https://github.com/jan-glx/patchKnitrSynctex in this directory.
Create an executable file "Knitr.engine" including the following shell scripts and put it in "~/Library/TeXShop/Engines/":
#!/bin/bash
# export PATH=$PATH:/usr/texbin:/usr/local/bin # already on my path!
Rscript -e "library(knitr); knit('$1')"
latexmk -pdf -pdflatex='pdflatex -shell-escape -synctex=1 -file-line-error' "${1%.*}"
Rscript -e "source('~/Library/TeXShop/Rscripts/patchKnitrSynctex.R', echo=FALSE, encoding='UTF-8'); patchKnitrSynctex('${1%.*}')"
In R, install the package "patchDVI".
In your .Rnw file, add "% !TEX TS-program = Knitr" on the top line of the document. Also inside the .Rnw document somewhere around the top of the document add an R code chunk
<<setup, include=FALSE>>=
patchDVI::useknitr()
… #any other knitr global setups
#
Happy knitting!

Resources