We now have a Twitter account @tex_tips you may like to follow to never miss a new blog post.
Month: April 2016
Use rotating.sty for wide tables/figures
You can use the rotating
package to automatically rotate wide figures or tables. It provides {sidewaystable}
/{sidewaysfigure}
as a alternatives for {table}
/{figure}
and rotates the environment content by 90°. The direction of the rotation can be set with package options clockwise
or counterclockwise
; if you set twoside
as class or package option the tables/figures are rotated by extra 180° on odd pages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
\documentclass{article} \usepackage{rotating} \usepackage{booktabs} \begin{document} \begin{sidewaystable} \centering \begin{tabular}{ l l l l l l } \toprule \textbf{No.} & \textbf{Name} & \textbf{Symbol} & \textbf{Atomic Mass (u)} & \textbf{Melting Point (K)} & \textbf{Boiling Point (K)} \\ \midrule 2 & Helium & He & 4.002602 & 0.95 & 4.216 \\ 10 & Neon & Ne & 20.1797 & 48 & 27.1 \\ 18 & Argon & Ar & 39.948 & 83.8 & 87.3 \\ 36 & Krypton & Kr & 83.8 & 116.6 & 120.85 \\ 54 & Xenon & Xe & 131.29 & 161.3 & 166.1 \\ 86 & Radon & Rn & 222.0176 & 202 & 211.4 \\ \bottomrule \end{tabular} \caption{A very wide table} \end{sidewaystable} \end{document} |
PS: Use booktabs
for a nicer styling of your tables 😉
Add a prefix to your labels
To keep track of (the target of) your labels you might want to add a prefix to their names indicating the thing they are referring to. E.g. sec
for sections, eq
for equations, fig
for figures etc. It is common to separate the prefix with a colon from the label name like in the following example:
1 2 3 4 5 6 7 8 |
\documentclass{article} \begin{document} \section{Test}\label{sec:test} % ... See section~\ref{sec:test} \end{document} |
Then when you read your code you always know that \ref{sec:test}
refers to a section.
In some cases the colon can cause troubles, e.g. when you type a french document with babel, varioref, hyperref and cleveref (see this and this post on TeX.SX for example). In that case you may use another character to separate the prefix.
JabRef 3.3 was released
Since 17th of April JabRef is available in version 3.3. Among other things with this updated comes an online help, faster search, improved Mac OS X support and a Swedish translation. The new version can be downloaded from jabref.org/#downloads.
scrpage2 is obsolete
With KOMA-Script version 3.12 the bundled package scrpage2
became obsolete and should no longer be used. Instead use the new package scrlayer-scrpage
, which provides nearly the same interface but has much more power than scrpage2
. For example with the new package instead of
1 2 |
\cfoot[\pagemark]{\pagemark} |
you can use
1 2 |
\cfoot*{\pagemark} |
to set the same mark content for plain and normal style pages. That is shorter and is more consistent: For instance if you have a more complex definition than \pagemark
now there’s only one place to make changes.
For more details check the manual: scrguien.pdf (or the german version scrguide.pdf), chapter 5.
When changing font size don’t forget the \par
When you change the font size with commands like \Large
inside a group you must end the paragraph either with a blank line or \par
before the group ends to get the right line spacing.
1 2 3 4 5 |
WRONG: {\Large some large Text} Normal sized text |
1 2 3 4 5 |
RIGHT: {\Large some large Text\par} Normal sized text |
TeX always uses the baseline skip settings of the site active at the paragraph end, which is actually \normalsize
in the first code.
Crossing lines
Sometimes it’s not possible in diagrams or charts, that a line goes over another one. To not let this crossing look like a real connection of the two lines, we can let the top line go over a gap in the bottom line. It would require to calculate the intersection of the lines.
However, there’s an easy way: draw the firsts line normally, then draw the top line with a bit whitespace around. Or the top line first thicker and white (or background color), then again thin and black (foreground color).
The probably easiest way in TikZ is: draw a “double” line (TikZ manual, 15.3.4 Graphic Parameters: Double Lines and Bordered Lines). Originally, it gives two parallel lines with space in-between filled by a color. But we want to get a single line. Solution: a double line in white, the gap between them filled in black. This results in a black line with white space around.
Many words, short and simple code example:
1 2 3 4 5 6 7 |
\tikzset{% link/.style = { white, double = black, line width = 1.8pt, double distance = 0.8pt }, channel/.style = { white, double = black, line width = 0.8pt, double distance = 0.6pt }, } |
This way, double distance
is the width of the actual black line, while line width
stands for the white space on each side.
Use it such as:
1 2 3 |
\draw [link, ... ] node1 -- node2; \draw [channel, ... ] node3 -- node4; |
It can look like:
Or:
Flipping images
When I defined an image as a TikZ node, I easily create and use a flipped version: I define a mirror
style that does a reflection for me:
1 2 3 4 |
mirror/.style 2 args = {path picture = { \node at (path picture bounding box.center) { \reflectbox{\includegraphics[width = #1cm] {#2}}};}}, |
As my original style was defined this way
1 2 3 4 |
Core1/.style = { image = {2.4}{nexus7k}, minimum width = 2cm, inner ysep = 1.5cm }, |
I simply override the image style by adding the mirror style:
1 2 |
Core2/.style = { Core1, mirror = {2.4}{nexus7k}}, |
This way I can get a diagram with a focus to its center:
Images as TikZ nodes
In diagrams, I need to include images, such as symbolic icons or photos of actual hardware. I defined a generic TikZ style with two arguments: the name of the image, and the desired width. The height comes automatically with the same aspect ratio.
1 2 3 4 |
image/.style 2 args = {path picture = { \node at (path picture bounding box.center) { \includegraphics[width=#1cm] {#2}};}}, |
For the final node style, I apply this style plus adjustments such as actual node width and x or y separation:
1 2 3 4 5 6 |
Core/.style = { image = {2.4}{nexus7k}, minimum width = 2cm, inner ysep = 1.5cm }, FEX/.style = { image = {2}{nexus2k}, minimum width = 2cm, inner ysep = 0.5cm}, |
All style definitions above go into a \tikzset command, separated by comma, plus many more global styles I need:
1 2 3 4 5 6 7 8 9 10 |
\tikzset{% ... Core/.style = { ... }, ... Label/.style = {black, font= \scriptsize, rounded corners = 2pt, fill=green!50, draw, thin, inner sep = 3pt}, TextLabel/.style = {rounded corners = 8pt, fill=blue!15, draw}, … } |
This way I get photos with annotations for larger physical diagrams: