Mittwoch, 17. Dezember 2014

layers in tikz in latex

To define a layer in tikz you would first in the preamble define a new layer with a defined name (here i chose "layer") and then set the order of the layers especially in comparison to the standard "main" layer.

\pgfdeclarelayer{layer}    % declare layer
\pgfsetlayers{layer,main}  % set the order of the layers (main is the standard layer)

in the plotting step you can then explicitly plot on a different layer by using the pgfonlayer environment

\begin{pgfonlayer}{layer}
      \draw[ultra thick] (0.0,0.0) -- ++ (1.0,0.0);
\end{pgfonlayer}

Freitag, 14. November 2014

latex trim image

to cut an image in latex you can simply use the trim option and then define then use the clip option as follow:

\includegraphics[trim={left low right top},clip]{./test.png}

Donnerstag, 23. Oktober 2014

Latex (tikz) emphasize part of graphic

http://tex.stackexchange.com/questions/181098/semitransparent-outside-clipped-region

gnuplot loop over all blocks in datafile

If you have a data file for gnuplot with indexable blocks (two blank lines between data blocks) e.g.:

0 1
1 2

0 1.5
1 2.5


0 2.0
2 3.0

and want to plot each plot individually but with the same plot command, you can simply use the gnuplot 4.6 stats function to get the number of blocks and loop over the blocks in a combined command:

#!/usr/bin/gnuplot

file="data.dat"
stats file

plot for [i=0:STATS_blocks-2] file index i w l ls i+1
PS: if you only have one blank line between data blocks you need to use the every command:
#!/usr/bin/gnuplot

file="data.dat"
stats file

plot for [i=0:STATS_blank] file every :::i::i w l ls i+1

Mittwoch, 22. Oktober 2014

histogram with gnuplot

if you want to plot a histogram with gnuplot, just use the raw data in "data.dat" and plot the data via the following command:

the data is transformed into a histogram via the bin function and then plotted via the plot with boxes command.

  #!/usr/bin/gnuplot

file="data.dat"
binwidth=1.0
bin(x,width)=width*floor(x/width)
set style fill solid 0.5

plot file u (bin($1,binwidth)):(1.0) smooth freq with boxes ls 1 t 'total'


the result looks something like that:

Freitag, 10. Oktober 2014

remove zero sized file

To delete zero sized files in bash you can simply use an if clause with you can put in a loop. First the if clause

  if [ ! -s file ] ; then
    rm file
 fi

and finally the full loop:

 for file in *
 do
   if [ ! -s $file ] ; then
     rm $file
   fi
 done

Dienstag, 24. Juni 2014

Redefine existing command in Latex

First you have to save your old existing \command to use it in the new redefined command via:
 \let\OLDcommand\command

Then you can redefine the exisiting command \command and use the old \OLDcommand:
 \renewcommand{\command}{this was \OLDcommand}

Sonntag, 1. Juni 2014

Latex write to a external file

To write to another text file in the latex document (FILE) you have to open it at the begin of the document and close it at the end of the document, like explained in the following:
\AtBeginDocument{%
  \Opensolutionfile{FILE}
}

\AtEndDocument{%
  \Closesolutionfile{FILE}
}

One can write to this file by using the following command:
\Writetofile{FILE}{text}

For example one could include the file at the end of the document by using:
  \IfFileExists{FILE.tex}{\input{FILE.tex}}{}

Define a new latex class

In order to define a new latex class you have to create a new file including the following statements as a header:
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{CLASSNAME}[CLASSCOMMENT]
\LoadClass{CLASS_TO_LOAD}

using this header a new class with name CLASSNAME is created (you can also define a comment CLASSCOMMENT) and by using \LoadClass all options of CLASS_TO_LOAD are loaded as predefined. Here as an example a dissertation class, based on the book class is defined:
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{dissertation}[class to write a perfect dissertation]
\LoadClass{book}

to load packages (e.g. graphicx) you need to use the following command:
\RequirePackage{graphixc}

in the follwing you can utilize
\newcommand{}{}
\renewcommand{}{}
 to define or change commands.

And use
\AtBeginDocument{}
\AtEndDocument{}
to execute commands at the beginning or end of the document.

Change latex command when compiling inside emacs

Emacs adds the following comment commands into a multiple latex file to know which commands to use and to define the master file:
%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% End:

In order to define the latex command e.g. include -shell-escape you have to add the following line before the End: command into the latex file:
%%% LaTeX-command: "latex -shell-escape" 

Latex define new "section-type"

To define a new section-type you need to use the titlesec Package:
\usepackage{titlesec}

to define a new section-type you use the titleclass command with name NAME and copy the properties of \PART (e.g. \section)
\titleclass{\NAME}{option}[\PART]

additionally you need a new counter and a command that gives back the counter:
\newcounter{NAME}
\renewcommand{\theNAME}{\arabic{NAME}} 

finally you can define the titleformat and titlespacings:
\titleformat{\NAME}[hang]
  {\normalfont }                      % format of titletext
  {{\bf Task \theNAME:} }      % command before titletext
  {0pt}                                      % spacing after titletext
  {}                                           % before code
  []                                           % after code
\titlespacing{\NAME}{0pt}{*4}{*2}

With options explained here:
\titlespacing*{command}{left}{before-sep}{after-sep}
For a more detailed description look into the titlesec documentatio, but these are the most important points.

Montag, 28. April 2014

make gnuplot produce a latex standalone document

gnuplot has a view very good terminals including the epslatex-terminal the only backdraw which i had recently was that the terminal produces either:
- a latex file which can be included in the latex-document
or
- a latex-document which produces a pdf with the minimal-documentclass
but it does not produce a document which can be used via includestandalone.

But now I found a way to do it.

1.  create example with epslatex terminal


set terminal epslatex
set output "test.tex"
plot sin(x)
set output

2. Add preamble for standalone figure and add \end{document}

Add at the beginning of test.tex:
 \documentclass{standalone}
\usepackage{graphicx}
\begin{document}

Add at the end of test.tex
\end{document}

PS: I also wrote a gnuplot script doing all of this:

epslatexterm(name, xsize, ysize, gnuplotcommand) = sprintf('\
name="%s";\
file=name.".tex";\
set terminal epslatex size %s, %s;\
%s;\
set output name.".tex";  \
replot; \
unset output;\
eval bash(''echo \" \\\\begin{document}            \" |cat - ''.file.'' > tmp    '');\
eval bash(''echo \" \\\\usepackage{graphicx}       \" |cat - tmp      > tmp2   '');\
eval bash(''echo \" \\\\documentclass{standalone}  \" |cat - tmp2     > tmp    '');\
eval bash(''echo \" \\\\end{document}              \" |cat tmp -      > tmp2   '');\
eval bash("sed ''s/".name."/".name."-inc/g'' tmp2 > ".file  );\
eval bash(''mv ''.name.''.eps ''.name.''-inc.eps'');\
eval bash(''rm tmp tmp2 -f'');\
set terminal wxt; \
',name,xsize,ysize,gnuplotcommand)

Mittwoch, 9. April 2014

set password for a pdf-file

To add a password to a pdf you can simply use pdftk and have it add a password as follows:

pdftk original.pdf output new.pdf user_pw PROMPT

You will get a prompt asking for the password, which in turns will be added to your pdf-file.

Freitag, 24. Januar 2014

imagemagick convert pdf to jpg

To convert a pdf file in a image file, the best option is to use imagemagick, which means convert. The only option you have to define is the density, which can be defined as follows:
convert -density 300 test.pdf test.jpg


Donnerstag, 16. Januar 2014

pdftk extract pages of pdf-file

Extract PAGES from input.pdf into output.pdf via:

pdftk A=input.pdf cat A${PAGES} output output.pdf

for more information use:
man pdftk

latex mix colors with colorlet and xcolor

To mix colors you need to have the xcolor package loaded first via:

\usepackage{xcolor}

And then you can mix two colors with

\colorlet{DarkBlue}{blue!80!black}
which means you mix 80% blue with 20% black. If you do not have a second color you mix the first with: "white".

Dienstag, 14. Januar 2014

latex tables with package booktabs

Latex tables

To use the latex booktabs package simply include:
\usepackage{booktabs}
in the header.

After this you can make a nice table via:
\begin{table}[h!]
  \begin{tabular}{ l l l}
    \toprule
    First & Second  & Third \\
    \midrule
    1 & 2 & 3 \\
    1 & 2 & 3 \\
    1 & 2 & 3 \\
    1 & 2 & 3 \\
    1 & 2 & 3 \\
    \bottomrule
  \end{tabular}
\end{table}



for further tips read the booktabs manual.