Tag Archives: renewcommand

Adding additional structure to the list of figures/tables

In large documents, you may end up with a pretty long list of figures or tables. In this post, I will show how to nicely structure these lists by adding subtitles. The whole thing turned out to be more tricky than I first thought, but more on that later. The my examples, I will show how to organize the list of figures by chapters. However, the code can be adapted and extended with little effort, including doing the same for the list of tables, organizing the lists by different levels (part, section) for the various documentclasses, the actual subtitle format (with/without name, page number), etc.

The main idea is to add a line to the list of figures whenever a new chapter is created. That’s relatively straight forward, we extend the chapter command by defining a custom command.

\newcommand{\newchapter}[1]{%
	\chapter{#1}
	\addcontentsline{lof}{chapter}{%
		Chapter \thechapter: #1 \vspace{10pt}
	}
}

The lines highlighted add a full entry to the list of figures. In order to omit the page number, use addtocontents as follows instead.

\addtocontents{lot}{\contentsline{chapter}{%
	Chapter \thechapter \vspace{10pt}}{}
}

This is all great and easy. The problem starts, however, when individual chapters have no figures. Then, the subtitle will still be printed, however, that there is no content. This is not exactly what we want. Therefore, we add a condition on when to print the subtitle and when not. The etoolbox package comes in handy here, it provides “if-then-else-like” statements. Here is the documentation. Briefly, whenever a new chapter starts, we set the parameter newchap to true. The subtitle is printed along with the first caption added to the list of figures. Therefore, if a chapter has no figure, the subtitle will not show up in the list.

First, we define the boolean parameter newchap:

\providebool{newchap}

Next, we define an alternative chapter command newchapter. This time, however, instead of directly adding the subtitle to the list, we only set the boolean parameter to true:

\newcommand{\newchapter}[1]{%
	\chapter{#1}
	\global\setbool{newchap}{true}
}

And finally, we redefine the caption command. The additional definition of shortcaption is necessary to support the optional caption argument, the alternative short caption for the list of figures.

\let\oldcaption\caption
\renewcommand{\caption}[2][\shortcaption]{%
\def\shortcaption{#2}
\ifbool{newchap}{%
	\addtocontents{lof}{\protect\contentsline{chapter}{%
		Chapter \thechapter \vspace{10pt}
	}{}}}{}
	\global\boolfalse{newchap}
	\oldcaption[#1]{#2}
}

If you never use the optional argument of the chapter command (chapter[alternative toc name]{real chapter name}) throughout your document, this is basically it, we are done.

In case you use the optional augment, however, we are not quite done yet, since we redefined chapter and by doing so omitted some of the functionality. We can fix this issue by redefining chapter in a different way, similarly to what we did for caption which brings additional advantages. On one hand, the code becomes more consistent and on the other hand, you won’t have to change chapter to newchapter throughout your document, since we just redefine the command rather than replace it. And here is the code:

\makeatletter
\newcommand{\saved@chapter}{} 
\let\saved@chapter\chapter 
\renewcommand{\chapter}{% 
  \@ifstar {\saved@chapter*}{\@dblarg\my@chapter}% 
} 
\newcommand*{\my@chapter}[2][]{% 
  \saved@chapter[#1]{#2}%
  \global\setbool{newchap}{true}
} 
\makeatother

I got this piece of code from this google group.

And here is what the final result looks like. Pretty cool, huh :-) .

List of figures with subtitles

The whole story only makes sense and works with numbered chapters, (i.e. when using the starred version: \chapter*{} you may run into problems or get undesired results, but you would have to try).

I am pretty sure there is an alternative, maybe simpler way to do the same thing. Maybe the tocloft package has a solution. Feel free to drop a comment and let me know about it. Thanks.


Date and time

I’m sure most of you have used \date{...} to print a date in a title (\maketitle) or letter. Probably you also know the more dynamic command \today to produce today’s date. Setting the date dynamically is not recommended (or even prohibited by some compilers) for usage in letters (i.e. \date{\today} or simply \date), since it makes tracing impossible.

Now what about time? Latex knows two packages that provide functionality related to time, datetime and KOMA-Script’s scrtime.

datetime:

\usepackage[option (see below)]{datetime}

\currenttime prints the actual time (hh:mm by default). In order to change the time format use:

\settimeformat{...}  with one of the following arguments:

xxivtime Twenty-four hour time, e.g. 22:28 (default).
hhmmsstime Twenty-four hour time, e.g. 22:28:00.
ampmtime Twelve hour time, e.g. 10:28pm.
oclock Displays the current time as a string, an interesting construct :-) .

You may also use any of these directly in the preamble where you load the package as an option.

The datetime package easily let’s you adapt the time separator:

\renewcommand{\timeseparator}{.}

The complete documentation can be found here.

scrtime:

\usepackage[option]{scrtime}

The option 12h=true can be used for the 12h-time-format. However, there is no am/pm that is automatically added to the time.

The current time (hh:mm by default) is produced using:

\thistime

The separator can be set as optional argument, e.g. \thistime[.]. Furthermore, using the form \thistime* will not produce a zero in front of the minutes in case the number is below 10.

Use \settime{} to set a constant value for \thistime. I’m not sure how to undo it though.

The complete documentation can be found here as part of the KOMA-Script documentation (in German).

Finally, both packages include commands for date-formating which I will not further describe here. Please see the docmentation for details. In general, the “datetime” package offers more functionality and therefore probably is the better choice in most cases, unless you are using the KOMA-Script anyway.


Header/Footer in Latex with Fancyhdr

Creating headers and footers in Latex can be done by using the package “fancyhdr”. This is a short introduction, showing the most important features of the package. If you know “fancyhdr” and are looking for something particular, refer to the fancyhdr-documentation.

First of all, you need to tell Latex to use the package:

\usepackage{fancyhdr}

and change the style from “plain” to “fancy”:

\pagestyle{fancy}

You will now the get the default fancy pagestyle which adds a line at the top of every page, except for some exceptions (title-page, abstract, new chapter in report).

Default fancyhdr page style:

Above the line, Latex will print headings:

Book/report

Left-hand side: section
Right-hand side: chapter
Note: if you use the optional documentclass argument “twoside”, Latex will alter the position of the section and chapter. (e.g. \documentclass[twoside]{report}, also introducing non-symmetric margins).

Article

For acticles, Latex will print the section only (chapters cannot be used with articles).
The footer only includes the page number which is centered by default.

Custom fancyhdr page style:

Even though fancyhdr has a default page style, you are free to define headers/footers yourself , which is not too difficult after all.

First you need to clear the default layout:

\fancyhead{}
\fancyfoot{}

There are seven letters you need to know before you can define your own header/footer:

E: Even page
O: Odd page
L: Left field
C: Center field
R: Right field
H: Header
F: Footer

Now it’s time to start defining your own layout. The definitions are added to the preamble:

\fancyhead[CO,CE]{---Draft---}
\fancyfoot[C]{Confidential}
\fancyfoot[RO, LE] {\thepage}

The decorative lines can be changed by increasing/decreasing their thickness (0pt means no line):

\renewcommand{\headrulewidth}{0.4pt}
\renewcommand{\footrulewidth}{0.4pt}

Note: According to the fancyhdr-documentation, the default layout is produced by the following commands:

\fancyhead[LE,RO]{\slshape \rightmark}
\fancyhead[LO,RE]{\slshape \leftmark}
\fancyfoot[C]{\thepage}
\headrulewidth 0.4pt
\footrulewidth 0 pt


Counters in LaTex

The following is a summary of counter usage and manipulations available in LaTex.

The following commands have a counter associated with it:

  • part
  • chapter
  • section
  • subsection
  • subsubsection
  • paragraph
  • subparagraph
  • page
  • equation
  • figure
  • table
  • footnote
  • mpfootnote
  • enumi
  • enumii
  • enumiii
  • enumiv

You have several possibilities to manipulate counters in LaTex:

  • \addtocounter {counter} {value} increments the specified counter by the value.
  • \newcounter {newcounter} [oldcounter] defines a new counter. Option ‘oldcounter’ is to link ‘newcounter’ to ‘oldcounter’.
  • \setcounter {counter} {value} sets the counter to have the specified value.
  • \usecounter{counter} used as the second argument of the list environment to number list items (see list command).
  • \value {counter} produces the value of the counter and is useful for doing arithmetic with counters.
  • \renewcommand {cmd} [args] [opt] {def} general command to define/redefine new commands.

Counter values can be printed in different formats such as:

  • \alph or \Alph {counter} for letters (a…z or A…Z) .
  • \arabic {counter} for arabic numbers (1…9).
  • \fnsymbol {counter} printing various symbols: asterisk, dagger, double dagger, section mark, paragraph mark, double vertical lines, double asterisk, double daggers, double double daggers. The value of counters must be between 1 and 9.
  • \roman or \Roman {counter} for roman numerals (I,II,III,IV,V,…,X,etc.)

Follow

Get every new post delivered to your Inbox.

Join 316 other followers