Outils d'utilisateurs

Outils du Site


tips:latex

LaTeX-related tips

Some not-oft used snippets for LaTeX.

Named labels for lists

Descriptions need a bit of work so the described name can be used in a \nameref.

\documentclass{article}
\usepackage{hyperref}
\usepackage{nameref}
 
\makeatletter
\let\orgdescriptionlabel\descriptionlabel
\renewcommand*{\descriptionlabel}[1]{%
  \let\orglabel\label
  \let\label\@gobble
  \phantomsection
  \edef\@currentlabel{#1}%
  %\edef\@currentlabelname{#1}%
  \let\label\orglabel
  \orgdescriptionlabel{#1}%
}
\makeatother
 
\begin{document}
 
\section{Definitions}
\begin{description}
    \item [Vehicle\label{itm:vehicle}] Something
    \item [Bus\label{itm:bus}] A type of \ref{itm:vehicle}
    \item [Car\label{itm:car}] A type of \ref{itm:vehicle} smaller than a \ref{itm:bus}
\end{description}
 
The item `\ref{itm:bus}' is listed on page~\pageref{itm:bus} in section~\nameref{itm:bus}.
 
\end{document}

(Try to) Count characters in a string

We want to create a command containing the character count of a string.

%% Character-counting code from [1]
%% [1] http://tex-and-stuff.blogspot.com.au/2011/03/counting-number-of-characters-in-tex_10.html
\def\gobblechar{\let\char= }
\newcount\charcount
\def\countunlessnil{%
  \ifx\char\nil \let\next=\relax%
  \else%
    \let\next=\auxcountchar%
    \advance\charcount by 1%
  \fi\next
}%
 
\def\auxcountchar{%
  \afterassignment\countunlessnil\gobblechar%
}
\def\countchar#1{\edef\xx{#1}\charcount=0 \expandafter\auxcountchar\xx\nil}
 
\def\shownumchar#1{%
  \countchar{#1}%
  There are \the\charcount\ characters in [#1].%
}
 
%% Do the counting of characters in #1 and store in \#2
%% XXX: I'm not sure this actually works
\newcommand{\countcharout}[2]{%
  \countchar{#1}%
  \expandafter\def\csname #2\endcsname{\the\charcount}%
}
\countcharout{\theAuthor}{theAuthorLength}
\countcharout{\theTitle}{theTitleLength}
\show\theAuthorLength

Code

C

  \lstdefinestyle{C}{language=C,
    extendedchars=true,
    tabsize=4, showstringspaces=false,
    numbers=right, stepnumber=1, numberstyle=\tiny,
    numberblanklines=false
    breaklines=true, breakindent=5pt, postbreak=\empty}

LaTeX and BibTeX

\lstdefinestyle{LaTeX}{language=[LaTeX]tex,
  morekeywords={maketitle, tableofcontents, vref, pageref,
    addbibresource, printbibliography,
    hypersetup,
    text, gather, align, eqref,
    verbatim, verbatiminput, verb,
    usetheme, frametitle, framesubtitle,
    algorithms, algorithm, algorithmic, WHILE, ENDWHILE,
    multirow, toprule, midrule, cmidrule, bottomrule,
    subsection, subsubsection, chapter, part, paragraph, appendix,
    figurename, tablename, lstlistingname,
    includegraphics, graphicspath, resizebox},
  emph={}, % Avoid inheriting BibTeX's emphs, for some reasson...
  %basicstyle=\scriptsize, breakatwhitespace, breaklines,
  basicstyle=, breakatwhitespace, breaklines,
  escapeinside={\%@}{@},
  %showtabs,tabsize=4,tab=\rightarrowfill,
  prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
  label=,caption=,
  frame=single, captionpos=b,
  %numbers=left,xleftmargin=2em,
  numbers=none}
\lstdefinelanguage{BibTeX}{keywords={%
  @article,@book,@collectedbook,@conference,@electronic,@ieeetranbstctl,%
  @inbook,@incollectedbook,@incollection,@injournal,@inproceedings,%
  @manual,@mastersthesis,@misc,@patent,@periodical,@phdthesis,@preamble,%
  @proceedings,@standard,@string,@techreport,@unpublished%
  },
  emph={abstract, address, annote, author, booktitle, chapter, crossref,
    edition, editor, eprint, howpublished, institution, journal, key, month,
    note, number, organization, pages, publisher, school, series, title,
    type, url, volume, year,%
  }, emphstyle=\sf,
  comment=[l][\itshape]{@comment},
  sensitive=false,
}

JSON

As per this post.

\colorlet{punct}{red!60!black}
\definecolor{background}{HTML}{EEEEEE}
\definecolor{delim}{RGB}{20,105,176}
\colorlet{numb}{magenta!60!black}
 
\lstdefinelanguage{json}{
    basicstyle=\normalfont\ttfamily,
    numbers=left,
    numberstyle=\scriptsize,
    stepnumber=1,
    numbersep=8pt,
    showstringspaces=false,
    breaklines=true,
    frame=lines,
    backgroundcolor=\color{background},
    literate=
     *{0}{{{\color{numb}0}}}{1}
      {1}{{{\color{numb}1}}}{1}
      {2}{{{\color{numb}2}}}{1}
      {3}{{{\color{numb}3}}}{1}
      {4}{{{\color{numb}4}}}{1}
      {5}{{{\color{numb}5}}}{1}
      {6}{{{\color{numb}6}}}{1}
      {7}{{{\color{numb}7}}}{1}
      {8}{{{\color{numb}8}}}{1}
      {9}{{{\color{numb}9}}}{1}
      {:}{{{\color{punct}{:}}}}{1}
      {,}{{{\color{punct}{,}}}}{1}
      {\{}{{{\color{delim}{\{}}}}{1}
      {\}}{{{\color{delim}{\}}}}}{1}
      {[}{{{\color{delim}{[}}}}{1}
      {]}{{{\color{delim}{]}}}}{1},
}

MATLAB

\lstdefinestyle{Matlab}{language=Matlab,
  extendedchars=true, tabsize=4,
  showstringspaces=false,
  numbers=left,
  breaklines=true, breakindent=5pt, postbreak=\empty}

The same with colours:

\usepackage{color}
\lstdefinestyle{MatlabCol}{style=Matlab,
  keywordstyle=\color{blue}\bfseries,
  commentstyle=\color{red}
}

Method B

(a subset thereof)

\lstdefinelanguage{b}{
  morekeywords={MACHINE, SETS, CONSTANTS, PROPERTIES, VARIABLES, INVARIANT, INITIALISATION, OPERATIONS, IMPLEMENTATION, REFINES, ANY, WHERE, THEN, END, VALUES, CONCRETE_VARIABLES},
  morecomment=[l]{//},
  morecomment=[s]{/*}{*/}
}
\lstset{language=b, tabsize=4, basicstyle=\small\ttfamily, breaklines=true, keywordstyle=\bf}

Background Image

One Page

\usepackage {graphicx}
\unitlength 1cm

\begin{picture}(0,0)
  \put(1.5,-23.2){
    \begin{rotate}{20}
      \includegraphics{confidentiel.png}
    \end{rotate}
  }
\end{picture}

Another option, text-only:

\usepackage {graphicx}
\usepackage{color}

\unitlength 1cm

\begin{picture}(0,0)
  \put(1.5,-23.2){\rotatebox{20}
      {\scalebox{9}{{\definecolor{Gris}{gray}{0.8}
          \textcolor{Gris}{Confidential}}}}}
\end{picture}

All Pages

\usepackage{graphicx}
\usepackage{fancyhdr}

\chead{
  \begin{picture}(0,0)
    \put(-7.5,-11.5){\includegraphics{SuperMouette_filigrane_nb.png}}
  \end{picture}
}

Tweak Margins

\usepackage{vmargin}
\setmarginsrb{GAUCHE}{HAUT}{DROITE}{BAS}{GAUCHE-TEXTE}{HAUT-TEXTE}{DROITE-TEXTE}{BAS-TEXTE}

Xfig

Generate PDFTEX_T with a Valid Boundingbox

$ fig2dev -L pdftex -B "0 0 0 0" SOURCE.fig DEST.pdftex
$ fig2dev -L pdftex_t -p DEST.pdftex SOURCE.fig DEST.pdftex_t

Adjust Labels for LaTeX Interpretation

  • normal text: Left-aligned, Roman, Normal;
  • math-mode ($): Centered, Roman, Special.
for file in *.fig; do
  cat $file | sed \
    -e "s/^4 [[:digit:]]\+ \([[:digit:]]\+ [[:digit:]]\+ -\?[[:digit:]]\+\) [[:digit:]]\+ \([[:digit:]]\+ [[:digit:]]\+\.[[:digit:]]\+\) [[:digit:]]\+ \([^\$]*\)/4 0 \1 1 \2 0 \3/" \
    -e "s/^4 [[:digit:]]\+ \([[:digit:]]\+ [[:digit:]]\+ -\?[[:digit:]]\+ [[:digit:]]\+ [[:digit:]]\+ [[:digit:]]\+\.[[:digit:]]\+\) [[:digit:]]\+ \(.*\$.*\)/4 1 \1 2 \2/" \
    > ${file}2
  mv ${file}2 $file
done

Use PDFTeXs in LaTeX

LaTeX
%% Preamble
\usepackage{graphicx}
\DeclareGraphicsRule{*}{pdf}{*}{}
%% [...]
%% Document
 \resizebox{\textwidth}{!}{\input{FILE.pdftex_t}}  

Change Section Style

\documentclass{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{calc}
 
\makeatletter
\renewcommand{\section}{\@startsection {section}{1}{\z@}%
{-3.5ex \@plus -1ex \@minus -.2ex}%
{2.3ex \@plus.2ex}%
{\@colorboxsection}}
\newcommand{\@colorboxsection}[1]{%
  \noindent\colorbox{black}{\Large\bfseries\makebox[\textwidth-1ex][l]{\textcolor{white}{#1}}}%
}
\begin{document}
\section{section}
 
\end{document}

Change Label Styles

The following can be done to change the way a counter is rendered; either using the rendering of more than one counter, or adding additional static elements.

\renewcommand{\theCOUNTER}{S.\theOTHERCOUNTER.\arabic{COUNTER}}

Cleanup CiteULike BibTeX export vith Vim

Let BibTeX take care of the proper capitalisation of titles.

Replace the opening brace on each line:

:%s/^\([^=]*title \+=[^{]*\){/\1"

and the last one:

:%s/^\([^=]*title \+=.*\)}\(,\?\)$/\1"\2/

Embed all fonts in a PDF

Using ps2pdf14 is often suggested around the web.

$ pdftops SRC - | ps2pdf14 -dPDFSETTINGS=/prepress - DST

However, converting to PostScript loses various things such as bookmarks and PDF links. Using gs directly seems to work better.

$ gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -dEmbedAllFonts=true -sOutputFile=DST SRC

Define new, tinier, font

\def\Tiny{\fontsize{4pt}{4pt}\selectfont}

Beamer

Unnumbered Backup Slides

It's always good to plan ahead of Q&A, and have backup slides. However, with numbered slides (as they *always* should be), counting the backup slides in could very well scare the audience (“Wow, still 60 to go, and the presentation ends in 10 minutes!”).

A quick backup environment solves the problem nicely.

\renewcommand{\appendixname}{Backup}
%% Based on [0]
%% [0] http://stackoverflow.com/questions/732902/ignoring-page-numbers-in-backup-slides
\newenvironment{backup}{
  \only<beamer>{\setbeamertemplate{footline}[default]}
  \newcounter{framenumberbeforeappendix}
 
  \appendix
  \section*{\appendixname}
  \label{sec:backup}
 
  \frame{
  \centering
  \Huge\nameref{sec:backup}
 
  }
 
  \setcounter{framenumberbeforeappendix}{\value{framenumber}}
 
  }{
  \addtocounter{framenumberbeforeappendix}{-\value{framenumber}}
  \addtocounter{framenumberbeforeappendix}{-1}
  \only<beamer>{\addtocounter{framenumber}{\value{framenumberbeforeappendix}}}
}

This is nicely complemented by sprinkling hypertext buttons to the relevant backup slides within the main body.

\newcommand{\button}[3]{%
\only<beamer>{\hyperlink{#2}{\csname beamer#1button\endcsname{#3}}}%
\only<handout>{{\footnotesize[#3 on slide \ref{#2}]}}%
}
\newcommand{\gotobutton}[2]{\button{goto}{#1}{#2}}
\newcommand{\returnbutton}[2]{\button{return}{#1}{#2}}

This way, a simple \gotobutton{see there}{frame:backup-here} would create a button saying “see there” pointing to the slide with \label{frame:backup-here}.

Note the conditional change in footline in the backup environment, and that in the definition of \button. This allows to generate nice handouts with the same code, where the button is replaced by a textual framenumber reference, and the frames are actually numbered within the backup too.

Adjust text-to-float ratio

%\renewcommand{\textfraction}{0.07}
\renewcommand{\topfraction}{1}
\renewcommand{\bottomfraction}{1}
\renewcommand{\floatpagefraction}{1}
\renewcommand{\dblfloatpagefraction}{2}
tips/latex.txt · Dernière modification: 2014/03/28 00:45 par oliviermehani