Donnerstag, 12. Dezember 2013

Bash combining files

Combine files side by side columnwise:

If you have two files one of them consisting of one column and another file consisting of one column as well you can join the files by using:
paste file1 file2

Combine files one after the other:

Simply use:
cat file1 file2
which will first print file1 and then file2.

The "join" command:

Additionally there is also the join command which will print data which is present in file1 and file2, e.g.:
join file1 file2
If file1 and file2 share the same string in one line it gets printed to standard out.

Mittwoch, 11. Dezember 2013

Gnuplot set linecolor from palette

In gnuplot you can set the color of a line in a plot by using:
 linecolor "COLOR" 

For example via, (linecolor is abbreviated via lc):
 plot sin(x) lc "blue"

But there are several others, including setting the linecolor from a palette, so according to the blog entry here and here. You set up a new color macro scheme and use it as a palette. This color palette you can then use to define the color of a line by using either the color of a colorbox (cb) value or a fractional value ranging from 0 to 1:
set palette @redgreenblue
plot sin(x) lc palette cb 1 
replot sin(x) lc palette frac 0.5 


More information can be found in gnuplot via:
help linecolor

Gnuplot user-defined color schemes

As already explained in this blog entry, this is the way to define color macros:
set macros
redgreenblue_small = "defined (\
                   0  0.0 0.0 0.5, \
                   1  0.0 0.0 1.0, \
                   2  0.0 0.5 1.0, \
                   3  0.0 1.0 1.0, \
                   4  0.5 1.0 0.5, \
                   5  1.0 1.0 0.0, \
                   6  1.0 0.5 0.0, \
                   7  1.0 0.0 0.0, \
                   8  0.75 0.0 0.0, \
                   9  0.5 0.0 0.0, \
                   10  0.25 0.0 0.0, \
                   11  0.0 0.0 0.0 )"

To use them in gnuplot you simply type:
set palette @redgreenblue


Periodically execute program (bash) - watch

To periodically execute a command, e.g. ll to see the change in files just use watch:
watch  [-n <seconds>] [--differences[=cumulative]] [--interval=<seconds>] <command>
For example like this:
 watch ls -l

Montag, 9. Dezember 2013

Gnuplot write in a variable from bash

If you want to read in a variable, which is in a file for example or you simply have a script to calculate it into gnuplot. Simply use the following command:

 variable="`BASH COMMAND`"

instead of BASH COMMAND you can use any bash command you like and even load variables or get data from files, e.g.:

 variable="`echo $VARIABLE`"

Dienstag, 3. Dezember 2013

Gnuplot multiplot - problems with replot

All those who use gnuplot multiplot probably know about the problems using replot in the multiplot environment. When using replot the lines which were plotted first and get replotted and appear darker or broader and if the xranges and yranges do not fit perfectly the plots get shifted.


For all that a small
 clear
statement in between the plot and replot might help.


For this i will show a full example in the following and two images of the two results with the png-terminal:
set terminal png
set output "test.png"
set multiplot

plot sin(x)
clear
replot cos(x)

unset multiplot 


without clearwith clear

Donnerstag, 28. November 2013

gnuplot array

How does it work to have an array in gnuplot?

The simplest way to create an array is like this:
 array="1 2 3 4 5"

to address a element you simple use the word command (for the zeroth element e.g.):
 word(array, 0)

In a real exercise you could use it e.g. to loop over files:
files="file1 file2 file3"
plot for [i=0:2] word(files,i) u 1:2 ti word(files,i)

gnuplot loop over datablocks

How to plot several datablocks with separate linestyles in one file?

In the following three datablocks are shown:

0.0 1.0
1.0 2.0
2.0 3.0

0.5 1.5
1.5 2.5
2.5 3.5

0.75 1.75
1.75 2.75
2.75 3.75

The first one e.g. can be addressed in gnuplot via:
plot 'datafile' every :::0::0

But to address all consecutively you need a loop the simplest way would be this loop over i=0,1,2:
plot for [i=0:2] 'datafile' every :::i::i w l ls i

There is also the possibility to use the do for loop with curly brackets {}:
 do for [i=0:3] {
 plot "datafile" ls i
} 

Freitag, 22. November 2013

emacs show latex index (reftex)

View index of latex document in emacs

1) Make sure reftex is properly installed


2) Add this section to your .emacs file

(require 'reftex)
(setq reftex-plug-into-AUCTeX t)
(add-hook 'LaTeX-mode-hook 'turn-on-reftex) 
(add-hook 'latex-mode-hook 'turn-on-reftex


2) Compile latex document


3) Showing the index in a new window


Ctrl-c =

Example:

emacs preview of texdocument pdflatex (auctex)

Show preview of latex formulas in emacs

1) install auctex with preview

2) add following to your .emacs file


(load "auctex.el" nil t t)
(load "preview-latex.el" nil t t)
(setq preview-gs-options '("-q" "-dNOSAFER" "-dNOPAUSE" "-DNOPLATFONTS" "-dPrinted" "-dTextAlphaBits=4" "-dGraphicsAlphaBits=4"))

3) Compile the preview via:

Ctrl-c p-d

Example:


Montag, 18. November 2013

latex documentclass standalone

To create a standalone image with latex, e.g. using tikz or pstricks, one should use the documentclass standalone. The Standalone document just contains the figure itself without the rest of the document.

 \documentclass{standalone} 

additionally several options can be added:

[tikz]
sets the standalone package as multi images and set every new tikzpicture environment on a seperate page:

 \documentclass[tikz]{standalone} 




additional Infos via standalone documentation

Donnerstag, 7. November 2013

gnuplot: redirecting print

Sometimes it comes in handy to redirect the output of the print command, especially if you fit something and want to reuse the data later. This can be done with gnuplot via:
 set print "FILENAME"  

A complete example:
set print "test.dat"
print "test"
set print  

Montag, 16. September 2013

latex: adding citations without citing

For one citation with name CITATION
 \nocite{CITATION}  

But it can also be used for all citations in the bibfile with:
 \nocite{*}  


bash "--" = no more options

This one is a bash tweak! If you need to tell a bash script, that there are no more options to come you can just add -- after all comments.
This is especially helpfull if a keyword after that has to start with a minus.

 rename -- -help help *  

renames the -help into help in all files.

Dienstag, 13. August 2013

Latex beamer uncover image function

Function for latex beamer

reserveandshow allows to uncover pictures in latex beamer on a certain slide, but still reserving the space via the \phantom command on the slides specified.
Only the none \phantom picture will be shown on the handout

 % reserveandshow! uncover figure at some point  
 \newcommand{\reserveandshow}[4]{%  
 \phantom{\includegraphics<#2|handout:0>[#3]{#4}}%  
 \includegraphics<#1|handout:1>[#3]{#4}}  


Usage:
\reserveandshow{1}{2-}[size]{image}  

Contour plot gnuplot

Contour plot

To create a contour plot like this two script files are needed. First of all a contour.gnu gnuplot file which plots the data and then contour.sh bash file which edits a temporary output file.

Steps in gnuplot:

  1. Write data into temporary file
  2. Write contour lines into temporary file
  3. Edit temporary file with contour.sh
  4. plot the edited data





Contour.sh


 #!/bin/bash  
 gawk -v d=$2 -v w=$3 -v os=$4 'function abs(x) { return (x>=0?x:-x) }  
   {  
       if($0~/# Contour/) nr=0  
       if(nr==int(os+w/2) && d==0) {i++; a[i]=$1; b[i]=$2; c[i]=$3;}  
       if(abs(nr-os-w/2)>w/2 && d==1) print $0  
       nr++  
   }  
   END {  if(d==0) {  
           for(j=1;j<=i;j++)  
           printf "set label %d \"%g\" at %g, %g centre front\n", j, c[j], a[j], b[j]  
       }  
   }' $1  

d: which option (0: set labels, 1: give data back where no label is)
w: width
os: offset

Contour.gnu

 
 # Step 1
 set xrange [-5:0]  
 set yrange [2:5]  
 set isosample 150, 150  
 set table 'test.dat'  
 splot sin(1.3*x)*cos(.9*y)+cos(.8*x)*sin(1.9*y)+cos(y*.2*x)  
 unset table  
 # Step 2
 set cont base  
 set cntrparam level incremental -3, 0.5, 3  
 unset surf  
 set table 'cont.dat'  
 splot sin(1.3*x)*cos(0.9*y)+cos(.8*x)*sin(1.9*y)+cos(y*.2*x)  
 unset table  
 reset
 # Step 3 and 4
 set xrange [-5:0]  
 set yrange [2:5]  
 unset key  
 set palette rgbformulae 33,13,10  
 load '<./contour.sh cont.dat 0 20 0'  
 plot 'test.dat' w ima, '<./contour.sh cont.dat 1 20 0' w l lt -1 lw 1.5  

Step 1:

Surface data of the function "sin(1.3*x)*cos(.9*y)+cos(.8*x)*sin(1.9*y)+cos(y*.2*x)" is plotted into "test.dat" in the x- and y-range given.

Step 2:

By using "set cont base" only the contour data of the function is written to the file "cont.dat"

Step 3:

By loading the contour.sh script the contour data in cont.dat is read and the labels for gnuplot are created.

Step 4:

The plot command plots first the data as an image and adds to that the modified contour data (modified by contour.sh)


adapted from http://gnuplot-tricks.blogspot.com/2009/07/maps-contour-plots-with-labels.html

Mittwoch, 31. Juli 2013

gnuplot tikz terminal

 Just now i found out about the tikz terminal in gnuplot 4.6 and just tried it out. It works very nicely, but I think I have to put a little bit of effort into it if its worth to change from pdflatex terminal to lua tikz.
But here is how it works:

Usage

 set terminal lua tikz standalone
 set output "test.tex"  
 plot sin(x)   

standalone/nostandalone/fulldoc

Compiling

 > pdflatex test.tex
immediate compiling only works as far as I saw for standalone and fulldoc

UPDATE COMING SOON

gnuplot if else

gnuplot 4.4 and lower:

 if (<condition>) <command-line> [; else if (<condition>) ...; else ...]
commands are just written after if condition

gnuplot 4.6

 if (<condition>) { <command>; <command>  
  <commands>  
  <commands>  
  } else {  
  <commands>   
  }  
 Commands enclosed in curly brackets.

string comparison:

test eq "test" 

And with this framework you can start building your logical setups in gnuplot, have fun.

gnuplot: key magic

All info about key: www.gnuplot.info

positioning key

 set key top left
top/bottom/centerleft/right

justification of the text

 set key Left
Left or Right (Default)

small tricks

 set key reverse            ## reverse text and sample
 set key samplen <length>   ## change length of samplen
 set key spacing <spacing>  ## set spacing between key lines

Dienstag, 30. Juli 2013

Changing textsize epslatex terminal or latexterm subroutine

 latexterm subroutine

Example file to change the fontsize in my latexterm subroutine:


The way it works:
1. Loading the gnuplot setup file
2. plotting a random function
3. evaluating the latexterm subroutine while renewing the normalsize in latex to another fontsize

 load "gnuplot-setup" 
 plot sin(x) 
 eval latexterm("test","6cm","4cm","",'\n\\renewcommand\{\\normalsize\}\{\\fontsize\{24.88\}\{30\}\\selectfont\}') 

epslatex terminal


In general you can also use it to change the textsize with the epslatex terminal for that you just have to add an option to the epslatex terminal like:

 set terminal epslatex header "\\renewcommand\{\\normalsize\}\{\\fontsize\{24.88\}\{30\}\\selectfont\}"

Imagemagick compose two images

convert pdf into png

convert -density 300 -depth 8 -rotate -90 -quality 85 test.pdf test.png  


compose an inlet into an background:
while resizing the inlet by 15% in both directions
and shifting the inlet by +700 +0
composite -verbose -geometry 15%x15%+700+00 inset.png background.png test.png  

gnuplot setup file

So here I am gonna show you my gnuplot setup file, which I load into every gnuplot script, I will put up a standard gnuplot script at some point.
This script can be loaded into gnuplot via:

load "/folder/of/your/gnuplot/script/script.gnu"

 

First part: Linestyles

 #--- LINESTYLES------------------------------------------------------- 
 set style line 1 lt 1 lc rgb "black"  pt 2 lw 2.0 
 set style line 2 lt 1 lc rgb "red"   pt 4 lw 2.0 
 set style line 3 lt 1 lc rgb "blue"  pt 6 lw 2.0 
 set style line 4 lt 1 lc rgb "#44AA44" pt 8 lw 2.0 
 set style line 5 lt 1 lc rgb "#cc7722" pt 10 lw 2.0 
 set style line 6 lt 1 lc rgb "#BBBBBB" pt 12 lw 2.0 

The first paragraph is just to set up a few linestyles in gnuplot which look good, so I set up 6 good looking linestyles, which can be used by typing:
 plot sin(x) ls 1 

Second part: Commands

bash("")

 #--- bash("command") ------------------------------------------------- 
 # -allows to use bash commands in gnuplot 
 bash(cmd) = sprintf('set print "| /bin/bash"; print "%s"; set print', cmd) 

which gives you an command to use bash easily in gnuplot:
 eval bash("echo test")


latexterm("","","","")

 #--- latexterm("name", "xsize", "ysize","command","latexcommand")--\  
 ----------------------------  
 # -sets terminal to epslatex and creates output file with xsize and\  
  ysize  
 latexterm(name, xsize, ysize, command,latexcommand) = sprintf('\  
 name="%s";\  
 xsize="%s";\  
 ysize="%s";\  
 command="%s";\  
 latexcommand="%s";\  
 set terminal epslatex size xsize, ysize color colortext standalone \  
 header "\\usepackage[T1]{fontenc}\n\\usepackage{mathptmx}\n\\usepackage{helvet} ".latexcommand command ; \  
 set output name.".tex"; \  
 replot; \  
 unset output;\  
 eval bash("latex ".name.".tex"); \  
 eval bash("dvips ".name.".dvi"); \  
 eval bash("ps2pdfbb ".name.".ps"); \  
 eval bash("rm -rf ".name.".aux ".name.".dvi ".name.".log ".name.".ps ".name.".tex ".name."-inc.eps"); \  
 set terminal wxt; \  
 ',name,xsize,ysize,command,latexcommand)   

which gives you an terminal which automatically creates an latex file and converts it into pdf (you need a ps2pdfbb script --> i will upload it later, but it can be also found online; the script corrects the wrong bounding box created by gnuplot).
 eval latexterm("test","6cm","4cm","","")


fitcube("","")

 #--- fitcube("plotinfo", "n")----------------------------------------- 
 # -fits a cubic function to the data plotinfo with the name "n" 
 fitcube(plotinfo,n) = sprintf('\ 
 %s(x)=a%s*x**3+b%s*x**2+c%s*x+d%s;\ 
 fit %s(x) %s via a%s,b%s,c%s,d%s;\ 
 print "fitted function is saved in %s(x): \ 
 %s(x)=",a%s,"x**3 + ",b%s,"x**2 + ",c%s,"*x + ",d%s;\ 
 eval bash("rm -f fit.log");\ 
 x1%s =(-2*b%s-sqrt(4*b%s**2-12*a%s*c%s))/(6*a%s);\ 
 x2%s =(-2*b%s+sqrt(4*b%s**2-12*a%s*c%s))/(6*a%s);\ 
 y1%s =%s(x1%s);\ 
 y2%s =%s(x2%s);\ 
 print "extreme points at (",x1%s ,"/",y1%s ,") and (",x2%s ,"/",y2%s ,") \ 
 and are saved in (x1%s /y1%s ) and (x2%s /y2%s )";\ 
 ',n,n,n,n,n,n,plotinfo,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n,n 
 ,n,n,n,n,n,n,n) 
Fits a cubic function to the function you say it to. It will save the maxima and minima into x1${functionname} y1${functionname} and x2${functionname} y2${functionname}. It allows a continues use of fitcube and storage of all needed parameters, if different names for the function are chosen.

Example to fit a dataset in file "data" with column 1 and 2 into the function named function:

  eval fitcube('"data" u 1:2',"function")

Third part: Macros

set macros 
 redgreenblue_small = "defined (\ 
           0 0.0 0.0 0.5, \ 
           1 0.0 0.0 1.0, \ 
           2 0.0 0.5 1.0, \ 
           3 0.0 1.0 1.0, \ 
           4 0.5 1.0 0.5, \ 
           5 1.0 1.0 0.0, \ 
           6 1.0 0.5 0.0, \ 
           7 1.0 0.0 0.0, \ 
           8 0.75 0.0 0.0, \ 
           9 0.5 0.0 0.0, \ 
           10 0.25 0.0 0.0, \ 
           11 0.0 0.0 0.0 )" 
 redgreenblue = "defined (\ 
           0 0.0 0.0 0.5, \ 
           1 0.0 0.0 1.0, \ 
           2 0.0 0.5 1.0, \ 
           3 0.0 1.0 1.0, \ 
           4 0.5 1.0 0.5, \ 
           5 1.0 1.0 0.0, \ 
           6 1.0 0.5 0.0, \ 
           7 1.0 0.0 0.0, \ 
           8 0.5 0.0 0.0)" 

Macros for 3d colormaps. If will give a tutorial on that in a later blogentry.

Sonntag, 28. Juli 2013

Hi

Hi,

I am a graduate student in the field of theoretical chemistry and with this blog I mostly just want to remember all the hacks I did for Gnuplot, Latex and all the other scripting languages I use.

Additionally I probably will put up a few setup files for gnuplot, latex, vmd, emacs and so on.

So if you are interested in that keep on looking =)

Best,

Koni