This is an old revision of the document!
Tips and Tricks for LaTeX
- For the redefining the header, you can use
\markboth– two parameters (left and right page) or\markrightwith just one parameter\markboth{\uppercase{Summary}}{\uppercase{Summary}}- You should note that a left-hand heading is generated by the last
\markbothcommand before the end of the page, while a right-hand heading is generated by the first\markbothor\markrightthat comes on the page if there is one, otherwise by the last one before the page. (source)
\chaptermark,\sectionmark, … withfancyhdrpackage- see http://en.wikibooks.org/wiki/LaTeX/Page_Layout for more details
- Useful page (and a figure) for lists:
- The if condition in TeX:
- construction if
\variableis empty:The variable \if#\variable# is empty.\else contains \variable.\fi
(\variablenaturally cannot contain'#')The variable \ifx\@empty\variable is empty.\else contains \variable.\fi
- comparing two strings:
\newif\ifsame \newcommand{\strcfstr}[2]{% \samefalse \begingroup \def\1{#1}\def\2{#2}% \ifx\1\2\endgroup \sametrue \else \endgroup \fi }- (The grouping is needed there to make '\1' and '\2' local but '\sametrue' global.)
- allocator
\newifis used in a sequention:
a declaration\newif\ifsomething
setting either\somethingtrueor\somethingfalse
and asking\ifsomething YES\else NO\fi
- foreach in TeX (thanks to TMA):
- If you want to
\dothis{}with each word in\foreach{Peter, Paul, John, Ringo}, you can do this as follows:\def\foreach#1{\first#1,,}\def\first#1,{\if,#1,\else\dothis{#1}\expandafter\first\fi}
- and then write something here:
\def\dothis#1{ … whatever #1 … }
- Aligning an element in the line according to whether it fits in the rest of the line (TeX)
- always right-aligned (from Petr Olšák's TeXbook naruby):
\def\alignelemright #1\par{\unskip \nobreak\hfill\penalty71\hskip2em\hbox{}\nobreak\hfill \hbox{#1}\par} - always left-aligned:
\def\alignelemleft #1\par{\unskip \hbox{} \hskip 0pt plus 1fil \penalty0 \hbox{#1}\hskip 0pt plus 1fill\hbox{}\par} - align to the right, if it fits, or to the left in the new line:
\def\alignelemrightorleft #1\par{\unskip \nobreak\hfill\penalty71\hskip2em \hbox{#1}\par}
- Two tables/figures side-by-side (source):
\begin{figure} \begin{minipage}[b]{0.5\linewidth} \centering \includegraphics[scale=1]{fn1} \caption{c} \label{f1} \end{minipage} \hspace{0.5cm} \begin{minipage}[b]{0.5\linewidth} \centering \includegraphics[scale=1]{fn2} \caption{c} \label{f2} \end{minipage} \end{figure} - Include completed PDF and add page numbers and other headings:
\usepackage{pdfpages}% settings \markright{Author Name} % or e.g. package fancyhdr \setcounter{page}{13} % number of first page % inserting (first page with pagenumber in the foot, others in the head with author name) \includepdf[pages=1, pagecommand={\thispagestyle{plain}}]{file.pdf} \includepdf[pages=2-, pagecommand={\thispagestyle{myheadings}}]{file.pdf} \textwidth,\linewidth,\columnwidth,\hsizeand others explained here (You want\linewidthin most cases.)- Useful packages:
- Source printing:
fancyvrb,listings fancyhdr- pseudocode
algorithmicx - trees:
xytree(complicated),dtree(Obo, pstricks),xyling(Martin P.,\Band\Tmust be relaxed) - tables:
xtab(better thanlongtableorsupertabular),tabulary(pretty wrapped text in autosized cells, similar totabularx),booktabs(no vertical lines,\toprule,\midrule,\bottomruleinstead of\hline) (all from this compendium)
longtableorxtabularenvironment cannot be insidetableenvironment. (Put it insidecenterinstead, since you cannot use\centeringas before.)- Higher cells/rows in tables can be achieved either by
booktabspackage, or (if you have to use vertical lines in tables) by inserting\toppaddingand\bottompaddingin every row:\newcommand\toppadding{\rule{0pt}{2.6ex}} \newcommand\bottompadding{\rule[-1.2ex]{0pt}{0pt}} ... \hline \toppadding First cell\bottompadding & Second cell & Third one \\ \hline ... hyperrefpackage:- If some links from ToC point to incorrect objects (e.g. section III.2.5 points to the page where the section I.2.5 starts), then the problem is in internal hyperref naming convention (both section in the example are represented as 'section.2.5'). You can change it either by
naturalnames=true(⇒ 'section.III.2.5'), orhypertexnames=false(⇒ 'section.289')
- New type of float and its List of
\usepackage{tocloft} \newlistof{graphs}{grp}{List of Graphs} % new list, file extension, its name \renewcommand{\cftgrptitlefont}{\sffamily\bfseries\huge} % style of list title \setlength{\cftbeforegrptitleskip}{10ex} % space above list title \setlength{\cftaftergrptitleskip}{4ex} % space below list title % previous three lines alternatively (in pbmlbook class): \cfttitleset{grp} \newlistentry{graph}{grp}{0} % one line of the list (because it will be of type 'graph', not 'graphs', due to float named 'graph') \renewcommand{\cftgraphfont}{\sffamily} % entry style \renewcommand{\cftgraphpagefont}{\sffamily} % page number style \setlength{\cftgraphnumwidth}{2em} % space for graph number % previous three lines alternatively (in pbmlbook class): \cftlineset{graph}{}{2em} \usepackage{float} \newfloat{graph}{ht!}{grp} % new float environment {graph}, "place it here or at the top of page" by default \floatname{graph}{Graph} % each caption is started as "Graph \thegraph: <captioin>" ... \begin{graph} \centering \includegraphics[scale=.8]{obr09} \caption{Graph Caption}\label{graph:first} \end{graph} ... \cleardoublepage\listofgraphs\phantomsection\addcontentsline{toc}{chapter}{List of Graphs} - Crop PDF figure by LaTeX
\graphicspath{{./images/}} \fbox{\includegraphics[scale=.4,trim=70 150 120 110,clip]{filename}}- it't left bottom right top and crop
./images/filename.pdf! fbox– for debuggingscaleorwidthorheight- other useful:
bb=10 10 10 10,angle=45,keepaspectratio,draft
- Special counter/style/… in enumerate
\renewcommand{\labelenumi}{\Alph{enumi}.} \setcounter{lastenumi}{\theenumi} \begin{enumerate} \setcounter{enumi}{\thelastenumi} ...
počítání slov (a dalších věcí)
texcount soubor.tex
texcount -inc soubor.tex … započítá správně slova i v souborech vložených v hlavním souboru pomocí \include
Podrobnosti v dokumentaci: texdoc texcount
Some PBML and SCTL-specific issues are here.
