The problem:

I want to show the proposed table of contents for my PhD thesis in my research proposal. I also want it to look ‘neat’.

Other, explored solutions:

I tried creating a separate document, then including only the contents page in the proposal. However, I could only get the contents page on a new page. (This looks silly - it leaves a large blank space under the Chapter heading). Other problems:

  • page numbers weren’t quite right

Solution:

  1. Create a second table of contents:

     \usepackage{tocloft}
     \makeatletter
     \newcommand\tableofcontentsA{
         \@starttoc{toca}
     }
     \makeatother
    
  2. Populate the new ToC:

     \addcontentsline{toca}{chapter}{Introduction}
         \addcontentsline{toca}{section}{Motivation}
         \addcontentsline{toca}{section}{Research Questions}
         \addcontentsline{toca}{section}{Summary}
     \addcontentsline{toca}{chapter}{Literature Review}
     ... etc ...
    
  3. Remove the title from the contents. This has been done in the definition of toca in (1). Another approach is to renew the contentsname parameter, removing it’s contents.

     \renewcommand\contentsname{}
    
  4. Single-space to save room:

     \begin{singlespace}
      ...  
     \end{singlespace}
    
  5. Print the ToC. Use two-columns (it didn’t fit on a single page otherwise). To make things prettier, use ragged edges (default balances), and then unbalance by n lines:

     \begin{multicols}{2}
     \raggedcolumns
     \addtocounter{unbalance}{5}
     \tableofcontentsA
     \end{multicols}
    

My Minimal, Working Example

Secondary, Multi-Column LaTeX Table of Contents (multi-column-toc.tex) download
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
\documentclass{report}

\usepackage{multicol} % Multicolum support
\usepackage{tocloft}  % ToC tuning
\usepackage{setspace}  % Single spacing

\makeatletter
\newcommand\tableofcontentsA{
    \@starttoc{toca}
}
\makeatother
\begin{document}

\chapter{Provisional Thesis Structure}

% Disable page-numbers in the ToC for these section types. 
% NOTE:: To be neat, these should be enabled again using \cftpagenumberson{chapter} etc. 
\cftpagenumbersoff{chapter}
\cftpagenumbersoff{section}
\cftpagenumbersoff{subsection}

% Now populate the ToC. 
\addcontentsline{toca}{chapter}{Introduction}
    \addcontentsline{toca}{section}{Motivation}
    \addcontentsline{toca}{section}{Research Questions}
    \addcontentsline{toca}{section}{Summary}
\addcontentsline{toca}{chapter}{Literature Review}
    \addcontentsline{toca}{section}{Background}

\renewcommand\contentsname{}
\begin{singlespace}
    \begin{multicols}{2}
        \raggedcolumns
        \addtocounter{unbalance}{2}
        \tableofcontentsA
    \end{multicols}
\end{singlespace}

\end{document}

I have a simple program:

int main() {
    char *c = "message";
    char *z = "message";

    if ( c == z )
        printf("Equal!\n");
    else
        printf("Not equal!\n");
    return 0;
}

Why does this print Equal!, even when compiled with optimisations turned off (-O0)? If both c and z point to the same area of memory, then won’t the first time I mutate z will be relatively expensive (requiring a copy-and-write)?

Multi-color prompt with Git information.

This post is taken from a StackOverflow answer for an interesting question.

Multi-color path in prompt

current_dir_path() {
    CURRENT=`dirname ${PWD}`
    if [[ $CURRENT = / ]]; then
        echo ""
    elif [[ $PWD = $HOME ]]; then
        echo ""
    else
        CURRENT=$(print -P %3~)
        echo "${CURRENT%/*}/"
    fi
}

directory_name() {
    echo "%{$fg_bold[cyan]%}$(current_dir_path)%{$reset_color%}%{$fg[red]%}%1~%{$reset_color%}"
}

Note: you may want to customise the colors, and possibly other parts of the given code. If you’re using somebody elses dotfiles, you probably don’t have a full understanding of what is happening here. I’ve provided a reasonably verbose answer below, to explain how I came up with this and what each section does. Hopefully that’ll help if you want to customize this (plus it was a learning experience for me, and it may help others learn).

The prompt.zsh file in Holmans repository sets the prompt. The current directory display is set in the directory_name() function.

What’s currently happening?

A quick discussion on how the directory_name() function works:

directory_name(){
  echo "%{$fg_bold[cyan]%}%1/%\/%{$reset_color%}"
}

We can see that the function echos a prompt-escaped sequence containing the current directory. In the echo statement:

  • %{$fg_bold[cyan]%}: sets the color to bold cyan.
  • %{$reset_color%}: Resets the color so whatever appears next in the prompt isn’t affected by this function.

This leaves us with:

%1/%\/

The %1 gives us the current directory. Read man zshmisc, under “Shell State” (specifically, the paragraphs explaining %d, %~ and %C) to discover why.

The %\ is not a prompt escape sequence, and the / is directly appended to the prompt.

Writing our own prompt functions

Your image suggests you want to display the current directory in one color, and it’s two parents in a different color. Here’s how I’d do it:

Step 1: Get the present working directory, including at most 2 parents.

We can use the prompt expansion sequence %3/. We’ll put it into it’s own function, to make life easier later on:

current_dir_path() {
    CURRENT=$(print -P %3~)
    echo "${CURRENT}"
}

directory_name() {
    echo "%{$fg_bold[cyan]%}$(current_dir_path)%{$reset_color%}"
}

-P causes print to do prompt expansion on the given string. We call our new function in the directory_name() function, applying colors (including the $reset_color). This gives us a prompt that looks like this:

$ ~ cd ~/repositories $ ~/repositories cd / $ /

Step 2: Remove the current directory.

Because we want to color the two sections of our current path differently, we’re going to split the current path in two. To do that, we’ll remove the current directory from what we’ve got so far:

current_dir_path() {
    CURRENT=$(print -P %3~)
    echo "${CURRENT%/*}/"   
}

{CURRENT%/*} will strip everything from the last /, including the /.
(See this question for reference (and another method to do this using dirname)).
Because we actually want a / between the current directory and it’s parent, we’ll reinsert a / ourselves.

Step 3: Append the current directory in a different color:

We’ll reuse %1/ from the original directory_name() for this.

directory_name() {
    echo "%{$fg_bold[cyan]%}$(current_dir_path)%{$reset_color%}%{$fg[red]%}%1~%{$reset_color%}"
}

I use %1~, because I prefer the ~ when I’m in $HOME.

Step 4: Test.

We have enough now to test. I’ve got a very minimal example using the function above, and this additional line:

export PROMPT=$'$ $(directory_name) '

Here’s a screenshot:

Example display.

We can see some issues: a ~/~ when in $HOME, and // when we’re in /.

Fixing these issues is a little troublesome. We’re appending a / (because otherwise we wouldn’t have a / between the current and parent directories). When in /, there is no parent, but we’re still adding in a /, which looks silly.

A similar thing is happening in ~, although I’m pretty sure it’s an example of named directories.

There’s probably a better way to fix this, but we’re just going to be nice and simple:

current_dir_path() {
    CURRENT=`dirname ${PWD}`
    if [[ $CURRENT = / ]]; then
        echo ""
    elif [[ $PWD = $HOME ]]; then
        echo ""
    else
        CURRENT=$(print -P %3~)
        echo "${CURRENT%/*}/"
    fi
}

We check whether dirname ${PWD} has given us a / or ~. If it has, to look pretty, we’ll echo an empty string here, and rely on the %1~ in directory_name() to give us a directory path.

And that’s it! This’ll give a multi-color path in prompt.

Show the root git directory (if we’re in a repo):

From this question, we can use $(git rev-parse --show-toplevel) to show the path to the top-level directory in our current git repo:

$ ~ echo "$(git rev-parse --show-toplevel)"                fatal: Not a git repository (or any of the parent directories): .git

$ ~ cd ~/repositories/config-files
chpwd(): Switching to profile: dotfiles
$ ~/repositories/config-files echo "$(git rev-parse --show-toplevel)"
/Users/simont/repositories/config-files

This will throw a fatal: Not a git repository if we’re not currently in a repo. To remove this error: pipe STDERR to /dev/null, but keep STDOUT:

echo "$(git rev-parse --show-toplevel 2>/dev/null )"

From this answer, we can get the path from the current git root to our current directory using some string manipulation:

${PWD#$(git rev-parse --show-toplevel)}

This will strip the path given by git rev-parse --show-toplevel from the path given by $PWD:

$ config-files/zshrc.d/themes echo "${PWD#$(git rev-parse --show-toplevel)}"
/zshrc.d/themes

Testing whether we’re in a git repository

If we’re not in a git repository, the prompt doesn’t need to change. We can test whether we’re in a repository:

if [[ -d $(git rev-parse --show-toplevel 2>/dev/null) ]]; then
    echo "In a git repo"
fi

The redirect (2>/dev/null) sends STDERR to /dev/null, so if we’re not in a git repo, zsh won’t spit errors at us.

Coloring the git root

We can use basename to get the git root without the preceding path:

$ ~/repositories/config-files if [[ -d $(git rev-parse --show-toplevel 2>/dev/null) ]]; then
    echo "$(basename $(git rev-parse --show-toplevel))"
fi
config-files

Let’s insert this into our current zsh prompt function:

current_dir_path() {
    CURRENT=`dirname ${PWD}`
    if [[ $CURRENT = / ]]; then
        echo ""
    elif [[ $PWD = $HOME ]]; then
        echo ""
    else
        # Check if we're in a git repo. 
        if [[ -d $(git rev-parse --show-toplevel 2>/dev/null) ]]; then
            # In a git repo. 
            BASE=$(basename $(git rev-parse --show-toplevel))
            echo "%{$fg_bold[magenta]%}${BASE}%{$reset_color%}/" 
        else # Do what we were doing before. 
            CURRENT=$(print -P %3~)
            echo "${CURRENT%/*}/"
        fi
    fi
}

If we’re in a git repo, this will give us the git root as the base of our path in magenta. One problem we’ll have is if $PWD and $(git rev-parse --show-toplevel) are the same (that is, when we’re in the root of the git repo). In this case, we’ll get a prompt that has this:

$ config-files/config/files

To fix this, we’ll merge the two functions (current_dir_path and directory_name), because they have no need to be seperate, and end up with this:

directory_name() {
    PROMPT_PATH=""

    CURRENT=`dirname ${PWD}`
    if [[ $CURRENT = / ]]; then
        PROMPT_PATH=""
    elif [[ $PWD = $HOME ]]; then
        PROMPT_PATH=""
    else
        if [[ -d $(git rev-parse --show-toplevel 2>/dev/null) ]]; then
            # We're in a git repo.
            BASE=$(basename $(git rev-parse --show-toplevel))
            if [[ $PWD = $(git rev-parse --show-toplevel) ]]; then
                # We're in the root.
                PROMPT_PATH=""
            else
                # We're not in the root. Display the git repo root.
                PROMPT_PATH="%{$fg_bold[magenta]%}${BASE}%{$reset_color%}/"
            fi
        else
            PROMPT_PATH=$(print -P %3~)
            PROMPT_PATH="${PROMPT_PATH%/*}/"
        fi
    echo "%{$fg_bold[cyan]%}${PROMPT_PATH}%{$reset_color%}%{$fg[red]%}%1~%{$reset_color%}"
}

export PROMPT=$'$ $(directory_name) ' 

Here’s a screenshot of the final result:

We have one final issue. We’re not showing the path between the git root and our current directory. To fix this:

Move all files from .tex.bak to .tex; ZSH specific:

man zshexpn, HISTORY EXPANSION, modifiers

for a in ./*/.bak; do mv $a ${a:h}/${a:t:gs/.bak//}; done

Replace all .\@ with \@. in every tex file, recursivly.

gsed -i.bak -E ‘s/.\@/\@./g’ */.tex

Use backreferences to replace \@.[A-Z] with \@. [A-Z].

gsed -i.bak2 -E ‘s/\@.([A-Z])/\@. \1/g’ */.tex

zsh

A useful function in the z-shell is zcalc. It needs to be autoloaded before use:

autoload -Uz zcalc
zcalc

To add hexdecimal numbers, set the default output to base 16 and prepend 0x to the inputs:

1> [#16] 0x1 + 0x5
16#6
zsh

Converting media filenames using zmv. Use zmv -n to check the conversion prior to actually doing it.


Filenames of the form:

1.1\ -\ City\ Of.mkv 

Convert to:

S1E1.City\ Of.mkv

using

zmv '(*)1.(*)' 'S1E${2// - /.}'

To shift multiple files from many folders into a new folder, ie this:

Foo\
 |---Season 1
 |     |--Extra.S1.name.avi
 |---Season 2 
       |--Extra.S2.name.avi

to this:

Foo
 |---Season 0
 |     |--Extra.S1.name.avi
 |     |--Extra.S2.name.avi
 |---Season 1
 |---Season 2

We can use the zsh globbing feature **, like this:

cd Foo && mkdir Season\ 0
mv **/*Extra* Season\ 0/

Renaming every file in a series of subdirectories matching a certain pattern.

Original File structure and naming scheme:

Foo
 |---Season 1
       |-- 1.1 - Foo.avi
       |-- 1.2 - Bar.avi
       |-- 1.11 - Baz.avi
 |---Season 2
       |-- 2.1 - Foobar.avi
       |-- 2.12 - Bazbar.avi

Desired file structure and naming scheme:

Foo
 |---Season 1
       |-- S1E1.Foo.avi
       |-- S1E2.Bar.avi
       |-- S1E11.Baz.avi
 |---Season 2
       |-- S2E1.Foobar.avi
       |-- S2E12.Bazbar.avi

Commands:

cd Foo    
# To match "x.x" numbering:
zmv -n '(**/)([0-9]).([0-9]*) - (*)' '${1}S${2}E${3}.${4}'
# To match "x.xx" numbering:
zmv -n '(**/)([0-9]).([0-9][0-9]*) - (*)' '${1}S${2}E${3}.${4}'

To fix double-dots in filenames (if filename has no name)

zmv '(**/)(*)' '${1}${2//../.}' 

After following the GitLab setup guide to install GitLab, I recieved Nginx errors. Running the recommended checks (section 6, “Check Application Status”), didn’t show any problems.

The Nginx errors said:

upstream prematurely closed connection while reading response header from upstream github

Examining the logfiles in /home/gitlog/gitlog/log, my unicorn.stderr.log was also displaying errors:

I, [2013-01-18T19:30:44.454863 #4806]  INFO -- : worker=0 ready
E, [2013-01-18T19:31:44.357351 #4652] ERROR -- : worker=1 PID:4693 timeout (31s > 30s), killing
E, [2013-01-18T19:31:44.380381 #4652] ERROR -- : reaped #<Process::Status: pid 4693 SIGKILL (signal 9)> worker=1
I, [2013-01-18T19:31:44.563039 #4809]  INFO -- : worker=1 ready
E, [2013-01-18T19:32:19.439897 #4652] ERROR -- : worker=0 PID:4806 timeout (31s > 30s), killing
E, [2013-01-18T19:32:19.466585 #4652] ERROR -- : reaped #<Process::Status: pid 4806 SIGKILL (signal 9)> worker=0

From this StackOverflow question, the solution is to increase the unicorn timeout to give GitLab time to generate the initial files the first time you log in.

convert is a useful tool which can be used to convert numerous files from .JPG to .png. Converting all files in a directory and subdirectories can be done using a for loop:

1
for i in **/*.JPG; convert $i $i:r.png

Resizing can be done at the same time. A % can be used (as well as various geometries):

1
for i in *.JPG; convert -resize 10% $i $i:r.png