Showing posts with label latex. Show all posts
Showing posts with label latex. Show all posts

Monday, February 18, 2013

Visualizing Dimens in Latex Documents

Rule based typesetting like LaTeX (or CSS for the matter) uses lots and lots of lengths that can be adjusted (or adjust itself in relation to some other quantity like the font size or other length). I believe there are over ten regulating how a list is typeset alone. If you typeset for a while you'll pick up a few over time, I'd say parindent and parskip usually come first, some are so obscure you'll never need them, some shouldn't even be touched, since you risk screwing your layout in ways that you thought only MS Word is capable of. For the middle category of the ones you don't know and still want to adjust I just found a great package. The layouts package enables you to actually see how they all play together for most major latex features. This is so insanely helpful and you don't actually need the package sice everything is reprinted in the manual of course. To see how great it really is see this picture for lists.

Sunday, December 9, 2012

Flexible inline enumerate

I was asked to produce an inline enumerate with more flexibility and i quite like the solution. Just include enumitem
\usepackage[inline]{enumitem}
and then define
 
\newenvironment{my_enum}[1]{\begin{enumerate*}[itemjoin={{; }}, itemjoin*={{; #1 }}]}{\end{enumerate*}}
\begin{my_enum}{as well as}
  \item foo
  \item bar
  \item baz
\end{my_enum}
.

Wednesday, October 17, 2012

changing the title of listings

This question cropped up in the channel yesterday and is quite obvious in the source 
but not so in the documentation. If you want to change the name of the listing (think
localization) then you need to change three things.
 
\renewcommand\lstlistingname{Quellcode}
\renewcommand\lstlistlistingname{Codefragmente} 
\def\lstlistingautorefname{Alg.}
 
The first line is the name at every listing, the second the title for the list of 
listings and the third if you want to autoref a listing.

Wednesday, September 26, 2012

xelatex, polyglossia - french and verbatim

An interesting problem with the combination of circumstances in the title has cropped up today. A minimal sample like this exhibits the problem:

\documentclass[a4paper]{article}
\usepackage{polyglossia}
\setdefaultlanguage{french}
\begin{document}
\verb|00:11:43:D4:86:A0|
\end{document}


 This code produces:
 whereas it correctly should look like this:
A bug about this has been reported, for now work around it with:
\newenvironment{\mverbatim}{\makeatletter \nofrench@punctuation\begin{verbatim}\makeatother}{\end{verbatim}\makeatletter\french@punctuation\makeatother}
The \verb is a little trickier, i'll think about that for a while. It probably boils down to copying the def from latex.ltx and adding an aftergroup or similar.
For now
\makeatletter\nofrench@punctuation
\verb|00:11:43:D4:86:A0|
\french@punctuation\makeatother

does the trick.

Wednesday, September 19, 2012

Widebar too wide

The following post shows a rather complicated solution to the problem of widebar being a little ugly that has naged for a while. Link

Sunday, September 16, 2012

Umlaute not working in lualatex/making unicode work

This is a problem, that has confused us about a year ago. Lualatex claims to have the inputlanguage set to utf8, still i couldn't enter umlaute äüöß into the document. They were always eaten up or so it seems.
What really confused me was, that it worked as soon as i used \usepackage[utf8]{luainputenc}. It also started working without the inputenc if i set the fontenc to T1. This seemed to make no sense at all.

What usually happens is: if you enter a char into the document that char is converted to unicode (conceptually, i don't think it's literally done that way), and then replaced by the glyph corresponding to that point in the output.
This isn't quite so with  some chars, umlauts being one example. They are often constructed as an accent (the ") on top of the normal char, so both packages activate a mechanism that replaces these by composited chars, ä by \"{a} for example. This magic is all done by fontenc and inputenc loads fontenc! If fontenc isn't loaded in some way, the character is output unchanged and the font doesn't have that symbol, so it omits it!

If the font happens to sport the symbol, → comes to mind, then it may or may not look different, because it isn't made up of several parts. If you want the best of both worlds you could either
  • use the ucs package which requires knowing the code point
  • use DeclareUnicodeCharacter{<unicode>}{<replacement>} which of course requires knowing the code point as well
  • make it active and redefine it.
    \catcode`\→=13
    \let→{\rightarrow}
If you don't know the code point, just google it, or use something like http://www.utf8-chartable.de/

Saturday, July 7, 2012

Multiple \label error

Package amsmath Error: Multiple \label's: label 'important' will be lost

It took me over an hour because there were a couple of valid causes to this problem and i had a few hunches that were all completely off. This error was caused by an incredible amount of tiredness on my part and the fact that I, in my stupidity, added a label to a ...
tada... align* environment. If you ever have this problem, think about if your environment actually has a tag you can reference! 

Rotating a single page in latex output

If you want to rotate a page (not just the content, i.e. you also change the geometry of that page!) the following is one possible solution: 
\documentclass{article} 
\begin{document}
  text

  \pagebreak[4]
  \global\pdfpageattr\expandafter{\the\pdfpageattr/Rotate 90}
  text\end{document}

Thursday, June 28, 2012

Getting the total number of pages into a counter

This is the only way i know of, to get the total number of pages for anything other than mere display. Took me two hours to get just right and requires two packages. There is a surprising lot of packages that do similar things but in the end all just print. Weird. Enjoy

\documentclass{article}
\newcounter{c@LastPage}
\usepackage{lipsum}
\usepackage{atveryend}
\begin{document}
a %\lipsum
%\lipsum
\makeatletter
\AfterLastShipout{\immediate\write\@auxout{\string\setcounter{\string c@LastPage} {\thepage}}}
\ifnum\value{c@LastPage}>2 foo \fi
\makeatother
\end{document}

Saturday, June 9, 2012

Wrap list around a figure

I often had a need for a way to wrap a list around a figure in the past and hadn't quite found out. Wrapfig and floatflt aren't compatible with lists and break the layout in very bad ways.

Approaches with 2 minipages or tabulars are just wrong, period. There is a new package called cutwin, that you can combine with parshape (or use it without) to coerce your text to do something similar to what ultimately wrapfig should do. I didn't get it to work properly with my ams doc, which is kind of sad. I get the feeling their way of building lists is incompatible with the rest of the latex world :/.

Then i found a solution in some mean way to use minipage. It was still wrong because the type area is so different with ams but after a bit of fiddling with my stuff this works:

\begin{enumerate} 
\item some text
% Rest of old \item
\par
% New \item starts here!
\noindent\begin{minipage}{.88\textwidth}
 \begin{wrapfigure}{r}{.3\textwidth}
   \centering
     \includegraphics[width=.29\textwidth]{pic}
  \end{wrapfigure}
\item[ad d)]
\end{minipage}
\end{enumerate}

Looks really wrong, but does the right thing. Hope this helps someone. I sure would have liked to find this.

A Proof list enumerated by Arrows

Up until recently i used

\begin{enumerate} 
  \item[``$\RightArrow$"] 
  \item[``$\LeftArrow$"] 
\end{enumerate}

or description instead of enumerate which looks even worse. Both don't exactly look right, are cumbersome to type and threw warnings about missing fonts, because latex tried to set the math in bold, so today i sat down and wrote the solution:  


\def\ProofDirection#1{\expandafter\@ProofDirection\csname c@#1\endcsname}
\def\@ProofDirection#1{\ensuremath{\ifcase#1\or\Leftarrow\or\Rightarrow \else\@ctrerr\fi}}
\def\RevProofDirection#1{\expandafter\@RevProofDirection\csname c@#1\endcsname}
\def\@RevProofDirection#1{\ifcase#1\or {$\Rightarrow$}\or {$\Leftarrow$}\else\@ctrerr\fi}

\newcounter{pcounter} 

\newenvironment{myprooflist}[1][]{% 
  
\ifx\relax#1\relax
        \let\@show@count\ProofDirection
     \else
        \let\@show@count\RevProofDirection
     \fi
  \begin{list}{\hss\llap{\textup{``\@show@count{pcounter}''}}}{\usecounter{pcounter}}
  }{
  \end{list}
}


Not only does it work and is cooler, it looks better and is less work to type. Nice!

Thursday, December 22, 2011

ams and enumerate (2nd. time round)

The enumeration problem has been nagging on me overnight and thus investigated further. The solution is simple: the enumerate package is plain incompatible with AMS (probably with anything other than standard latex).

As you can see ams changes a lot of widths including parsep, parindent and leftmargin. The enumerate package also changes leftmargin, unfortunately not relative to the one set but absolute, probably calculated from the standard. So don't use enumerate and AMS! I will define my own list environment from now on which looks better.

Amsthm, lists and enumerate.

Problem:
Amsthm has three theorem styles normal, remark and definition, of which only the normal style is set in italics normally. If you use a list within such a theorem (questionable according to some, necessary to me) then the item labels for these lists are italicized as well which doesn't look good.

The advice to correct this was to manually \textup the label in each item. This of course works normally, but i also employ enumerate. In this case i got a warning "The counter will not be printed.", which is due to the fact, that enumerate will not detect counters that are inside braces.
Solution:
\begin{enumerate}[\upshape i)]
\item...
\end{enumerate}

This finally looks good

Disjoint Unions

I had countless revelations about math and LaTeX over the last week. I don't know why this happened but i made a major step forward. Most of them are little but they add up and it shows. One of the things that I fudged together years ago was an ugly construction for disjoint unions, something along the lines suggested in a4symbol.pdf. The correct solution however is to \usepackage{MnSymbol} and use \cupdot/\bigcupdot!

Tuesday, December 20, 2011

Latex macro reference for authors

I just found this handy reference for package authors, that I didn't know so far. Try it here.

Friday, May 27, 2011

Synctex

I recently became aware of Synctex the successor of pdfsync.
pdfsync is a technology that allows synchronization between a (la)tex file and the corresponding pdf output. It wasn't the first of its kind but certainly the best i've tried. In practice you need support by both your viewer and editor but then it's down to pressing a command or (shift/ctrl/whatever)+clicking somewhere and you are magically teleported into the other app to the spot corresponding to the one you were at on the other side. Anyone who has texed and needed to proof a doc of more than 5 pages length before will understand that this is invaluable.


For all the goodness, this had some drawbacks unfortunately: pdfsync was based on some tex primitives as opposed to being embedded into the engine, so the output under some circumstances wasn't perfect, it could change the generated rendering and it was incompatible with some popular packages (e.g. colortbl). I have never been bitten by any of these in the last 3 years but it always was a good excuse for viewers not supporting it (they are usually the end that carries the burden).
It unfortunately still has it's place as we will see shortly, so here are links to set up  forward  and backward search underMacOS + vim + skim, evince doesn't have it and never will which leaves you with xpdf and okular on linux, both of which aren't my favourites.

In comes synctex: It is the natural evolution of pdfsync done by the original author and has all the goodies without the down sides. Setting it up under Mac was even easier now that skim supports macvim as a preset :). Just choose MacVim under preferences>sync and put the following in your .vim/ftplugin/tex.vim (assuming you use the vim-latex suite)

let g:Tex_DefaultTargetFormat = 'pdf'
let g:Tex_CompileRule_pdf = 'pdflatex -synctex=1 --interaction=nonstopmode $*'
let g:Tex_ViewRule_pdf = 'Skim'
 
Look at this for all other MacOS related progs, mupdf, and evince. Okular just needs to be called with
okular --unique 'file.pdf#src:linenumber file.tex'
(That's what i think the evince people should have done instead of using dbus. Oh, well...)

Now for the bad part, lua(la)tex + evince/okular isn't working with synctex. I filed bugs with luatex, evince and okular but i don't expect a solution for this anytime soon. :( That means i am back to pdfsync for now, since i really, really like my luatex. So have fun with that!

Edit: you need to tell vim to call okular/evince with the exact same filename that lualatex produces which is ./file
In order to do that goto compiler.vim which if you installed vim-latex in your .vim is under .vim/ftplugin/latex-suite/compiler.vim otherwise probably somewhere in /usr.
Apply this patch for okular. For evince it really depends which script you use, but read the patch and do it analogously.

Thursday, January 13, 2011

Tex Font Catalogue

This is a most excellent find:

http://www.tug.dk/FontCatalogue/

Saturday, January 8, 2011

Latex Wrapperclass

When you define a new class in latex you most unlikely want to start from scratch. This is of course not a problem, you can easily inherit another documentclass. Problems tend to start if you also need to react on an option of the class as well. The example shows the easiest way i know of:

\NeedsTeXFormat{LaTeX2e} 
\ProvidesClass{myartcl}[2010/01/06 This is so 2010!]
\RequirePackage[unknownkeysallowed]{keyval} 
%set up the function for finding doublesided as an argument 
\define@key{keygroup}{doublesided}[true]{% 
  % Do whatever you need to here 
  \PackageWarning{myartcl}{Found doublesided #1}% 
} 
% and we pass it on to the keyval parsing algo 
\DeclareOption*{% 
  \edef\act{\noexpand\setkeys{keygroup}{\CurrentOption}}\act% 
  \PassOptionsToClass{\CurrentOption}{article}% 
} 
\ProcessOptions\relax 
\LoadClass{article} 

Saturday, January 1, 2011

Optional Arguments in Latex Functions, Style and Class Files

I just wrote my first own latex package and as usual i was rather aiming for the stars than KISS.
The last big issue was finding out how to have optional package arguments that take a value (think geometry's left).

First: 
How to have functions with more than one optional argument or named optional arguments: Turns out not to be too complicated in the end. First you need the package keyval which enables you to parse key=value style arguments and then you need to understand how it works (the doc is aweful imho). Set up your parsing functions with something like

\define@key{keygroup}{keyname}{\dostuff{#1}}

this defines an option keyname that takes an argument, that has to be provided if the key is used.
Example: 
\define@key{keygroup}{color}{\domorestuff{#1}}

You can also define an optional argument that has an optional value that gets used when none is provided with the following syntax

\define@key{keygroup}{keyname}[default]{\dostuff{#1}}

 This lets you for example define a length type argument like follows:

\define@key{head}{width}[\textwidth]{\def\@bannerwidth{#1}}

This would set \@bannerwidth to 5cm for width=5cm and to the width of the text otherwise.
 Now to parse an optionstring you need to pass it to \setkeys like this:

\setkeys{keygroup}{optstring}

So now in order to have a command with more than one optional argument (beware syntax is different though!)

\newcommand{\printbanner}[2][]{
    \setkeys{keygroup}{#1} 
    ....
}
and call it like so: 

\printbanner[width=18cm,color = red]{Other Arg}
 
Second:
I think keyval was written with functions in mind but that shan't stop us from abusing it for package options. Declare all your options that don't need keyval syntax first (it's easier than going through keyval). Afterwards set up the catchall handler with 

\DeclareOption*{\edef\@act{\noexpand\setkeys{keygroup}{\CurrentOption}}\@act}

The reason you need this is so you can delay the expansion of the arguments. This should be enough so you can 

\includepackage[width=18cm]{package}

Further Notes:
If you want a switch case like option have a look at the package ifthen.
A complete, minimal, working example is available here.

Monday, May 3, 2010

big/small delimiters fontsize

Ok this is old news but i am sick of googling it now.

Delimiters can be sized manually (i.e. not with \left \right) with \big \Big \bigg \Bigg. The sizing of these is adjusted (to the better when loading the amsmath package)

Non delimiters (and this includes Integrals and Sums) can be resized with \tiny
\scriptsize
\footnotesize
\small
\normalsize
(default)
\large
\Large
\LARGE
\huge
\Huge