2012年11月22日星期四

Font Selection in Beamer and Normal Tex Files

When I was trying to figure out how to change the font for the complete beamer presentation, I found the following two commands in the Manhattan Beamer Theme:

\renewcommand\sfdefault{phv}
\renewcommand\familydefault{\sfdefault}

Basically they give a new meaning for "\sfdefault" and "\familydefault" respectively. As a result, both "\sfdefault" and "\familydefault" are set to "phv".

I tried to eliminate the second command while keep the first one, the font in the beamer would be changed as well, however, not the other way around. On the other hand, for a normal LaTex article, only the second command by itself will change the font for the whole document, but not the first.

So what is "\sfdefault", "\familydefault", why do they have different effect for beamer and normal article? And what does "phv" stand for?

After searching for LaTex font setting online, I realized the font system is more complicated than I expected. There are essentially five attributes for fonts in LaTex: encoding, family, series, shape, and size. "phv" is a value for the family attribute, which denotes Adobe Helvetica, further details can refer to the references I put at the end.

As to "\sfdefault", actually there are two other commands in a kit: "\rmdefault" and "\ttdefault". In the above kit, “sf” stands for serif, "rm" stands for roman, and "tt" stands for typerwriter. Three of them also have corresponding commands respectively:
"\textsf", "\textrm" and "\texttt". So normally when we choose a template, the font families for "\textsf", "\textrm" and "\texttt" are already predefined when the template is fixed. So using "\renewcommand" is just to update the original value for each case. "\familydefault" is another macro denoting the system default font for the complete document. Hence, for instance, after using the following command:

\renewcommand\ttdefault{phv}

all texts covered by the "\texttt" will have the "phv" font.

The reason why "\renewcommand\sfdefault{phv}" changes the font for beamer but not normal article is because beamer use the value of "\sfdefault" for "\familydefault", while normal article use the value of  "\sfdefault" for "\familydefault". Therefore, only "\renewcommand\sfdefault{phv}" is enough to change font for beamer, only "\renewcommand\rmdefault{phv}" is also enough for normal article.



Reference:
http://www.latex-project.org/guides/fntguide.pdf

http://www.tug.org/pracjourn/2006-1/schmidt/schmidt.pdf

2012年11月21日星期三

Multiple Optional Arguments for New Command Definition

The normal command defining methods, namely the "\newcommand", "\renewcommand", and "\providecommand", provide a optional argument session, where we can provide a default value for an argument, if the argument is not explicitly specified. Usually, the default value is used for the first argument "#1".

If you want to add more than one optional arguments, the following straightforward extension would cause a error:

\newcommand{\def}[3][default]{\textit{#1}+\textbf{#2}-\texttt{#3}}

One possible solution is to use the "relaying technique", which embeds one command into another, both of which contain an optional argument. As a result, the superior command will take two optional arguments to outward seeming. However, it is just a computational trick.

Also, attention should be paid when using the above method: the inferior command must be set at the end of the command definition for the superior one; its argument will also be attached after the argument of the superior one. Following is a simple example:


\newcommand\ABC[2][default1]{#1+\textbf{#2}}
\newcommand\DEF[1][default2]{#1-\ABC}

Then:


  • "\DEF{obligatory}" yields "default2-default1+obligatory"
  • "\DEF[real2][real1]{obligatory}" yields "real-real+obligatory"
  • "\DEF" yields an error

Defining Your Own Command in LaTex

LaTex provides a bunch of useful integrated commands, such as "\textit{}", "\begin{block}...\end{block}", etc. However, you might encounter the situation in which a self-defined command would be more convenient. There are three standard methods in LaTex for creating new commands:

  1. \newcommand{cmd}[req_args][opt_args]{def}
  2. \renewcommand{cmd}[req_args][opt_args]{def}
  3. \providecommand{cmd}[req_args][opt_args]{def}
The three above methods contain the same syntax, where "cmd" stands for the name of the command, it should usually start with a backslash; "req_args" represents the number of arguments the new command carries, if unspecified, then the command takes no argument; "opt_args" denotes the default values of some arguments, if they are not presented when applying the command; finally, "def" is the detailed definition/description of the command.

The three commands are very similar in function, but they have to be used in different conditions. "\newcommand"  helps to update the meaning of an already existing command, while "\newcommand" and "\providecommand" can only be applied to construct *new* commands, that's to say, the ones that do not exist before. If applied to an existing command,  "\newcommand" will yield a compiling mistake, while "\providecommand" simply does not do anything.

If you are creating commands with arguments, then you have to specify the number of arguments in the "req_args" part, and refer each argument with anchors such as "#1", "#2", "#3", etc., the number corresponds to the linear order of arguments.

The "opt_args" is optional, just in case when the argument is not explicitly mentioned, some default value will take its place.

Some examples of new commands:
  • \newcommand{\abc}{hello world!}
  • \newcommand{\def}[2][default]{\textit{#1}+\textbf{#2}}
  • \newcommand{\def}[3][default]{\textit{#1}+\textbf{#2}-\texttt{#3}}
  • \renewcommand{\abc}{Bonjour Tout le Monde!}

Reference:

2012年11月20日星期二

Display All References in The Article

As we know, the BibTex under LaTex is one of the most proficient tools for handling bibliography. Normally we only want the reference session to include the specific items which we have cited in the article. However, in some cases, we would like to have a more elaborate control over bibliography. For instance, when making presentations with Beamer class, there might not be enough space or not necessary in the slides to cite each reference, but we still want the un-cited reference to appear at the end. Then the following code could be useful:

\nocite{*}

Just put it before the bibliography style declaration.

2012年11月12日星期一

Kindle关于中文格式(TXT,MOBI,PDF)的若干问题

对于使用Kindle来阅读中文的用户而言,如果手中的电子书是PDF格式的,阅读起来是没有什么问题的(比如乱码),因为PDF就像图片一样。但是对于MOBI或者TXT而言,情况就不太一样了。

1. 分行号和分段号

一般大家从网上下载的电子书都是TXT格式的,仔细观察就会发现许多TXT的分段都不是很清晰。一方面是因为这些TXT大部分都是网友手动输入的,难免会有一些错误。另外一方面是大家使用的编辑软件不一样,造成的分段号(paragraph break)和分行号(line break)混淆。这样一来就会出现很多TXT的右边一大片空白,文字不会自动延伸(很多电子邮件系统也具有这个特性)。

一开始我打算编一个简单的文字处理小程序自己将所有的分行号(line break)删除。后来发现网上有现成的工具,效果还非常不错:

http://www.textfixer.com/tools/remove-line-breaks.php

用这个工具处理过之后,再拷贝到Kindle里面,就不会出现大范围空白了,所有的空间都被文字填满了。

注意,这些分行号不会随着TXT转化成其他格式(PDF或MOBI)而消失,只有进入原始的TXT强行删除他们才不会影响阅读。

2. TXT转化成PDF后的空白页面

有一些TXT文件用专门软件转化成PDF格式之后,导入Kindle后会发现很多页面都成空白了。但是同样的PDF在电脑中阅读却不存在一样的问题。

Mac下的解决办法是用“预览”打印该PDF,然后再保存为PDF。不过因为我已经基本放弃了在Kindle阅读转化而来的PDF,这个方法现在用得不多了。

3. MOBI中的乱码

如果你是从亚马逊上直接购买的MOBI,应该不存在乱码。我这里指的乱码是用特殊工具(比如calibre)将TXT转化成MOBI过程中出现的。

不知道是不是MOBI在亚马逊中支持的字体有限,还是转换工具中的bug,在TXT中完美显示的中文到了MOBI格式下总会出现不少乱码,具体的形式就是方框,或者方框里面一些乱七八糟的内容。

我的解决办法是直接使用TXT,但是不是拷贝到Kindle里面,而是通过发邮件到用户的亚马逊网上账户(推送功能)。网上账户接到了发送的TXT以后,会进行一步转化,将文件转成更容易被Kindle接受的模式。一个有趣的现象是转化后的文件作者信息会更改,你的账户名将用作新的TXT的作者。如果你非常不喜欢这点,可以考虑直接拷贝,不过这样一来,就得承担更多乱码的风险。

注意,如果文件是通过推送功能发送到Kindle设备上的话,亚马逊的服务器会改变文件的格式,不论初始文件是TXT或者PDF,最终通过wifi下载的文件都是AZW或者AZW3的。这个不是很普适的格式似乎只有在Kindle中才能完美打开。所以,建议大家推送到Kindle以后继续保持源文件。

Kindle文件格式

总体而言,Kindle支持最好的格式是MOBI,也即是直接从亚马逊网站上买书时候的格式。同时,Kindle对于PDF和TXT也有很好的支持。这里有一个不错的电子书阅读,格式转换软件推荐给大家:calibre通过这个软件,用户能够给自己电脑里面的所有电子书做一个很好的索引(index),并且各种格式之间的转换也十分容易。

阅读PDF和MOBI或者TXT最大的区别就是PDF文件在Kindle中丧失了一些属性。比如使用调整文字大小的功能,PDF文件只是单纯地放大,像图片放大一样,随着字体的增大,画面也会越来越模糊。至于更改字体,就更是不可能了。所以我个人更加倾向于在Kindle上使用MOBI或者TXT,即使是PDF也要是用Latex生成的。

2012年11月11日星期日

Accent Symbols in LaTex (e.g., French, German...)

Often that one might encounter the situation where certain accent symbols are need to be input in a LaTex file, such as the "é", "à", "œ" in French, and "ß", "ü", "ä" in German.

A direct, however tedious and complex way is to use the LaTex embedded command mechanism. Different commands (a backslash together with other strings) represent different symbols. For instance, "\'{}" stands for a grave accent, "\`{}" for a acute accent, "\^{}" for a circumflex... detailed mapping could be found at Latex/Accent Wikibooks. This method is easy to comprehend and apply, also seems natural for those who are familiar with the LaTex command style. However, if you are dealing with a document completely in another language, where there are a large number of accent symbols, it will become far more tiring to assign each symbol a correspond command. Further more, one disadvantage of this method is that when copying the text in the generated PDF file, the accent symbol will not be displayed as verbatim characters, which causes immense inconvenience when you want to post-process your file (e.g., Google Translate).

There exists other ways to handle multi-language LaTex of course. One usual one is to add some language package and then typeset with XeLaTex. This is quite helpful for non-latin languages such as Chinese and Japanese.

But if your document is restricted within the domain of Latin languages, one very simple way is to set the input encoding to UTF8, and the font encoding to T1, which supports some extended font sets. Just put the two lines in the preamble,


\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}


then you can directly input almost all Latin symbols, including those accent ones. Finally, just by conducting the normal LaTex compilation, all symbols will appear as how they should look like. You might find the UTF8 symbols look a bit different from the one generated with the LaTex command. In my opinion, which style is preferred is really a personal taste, the important thing is that they can be displayed correctly.

2012年11月8日星期四

Define Your Own Beamer Theme

Following from the last post, this article aims to help you create your own Beamer Themes, in case you are bored of all existing ones (including the two key-note styles I referenced).

After a brief investigation of the Beamer predefined themes directory, I realized that every beamer theme is made up of the four following components:

  1. Color (albatrossbeaverbeetle, ...)
  2. Font (defaultserifstructurebold, ...)
  3. Inner (circlesdefaultinmarginrectangles, ...)
  4. Outer (infolinesshadowsidebarsplit, ...)
Each of the above has a bunch of available options that you can apply. Beamer Theme Gallery contains a dynamic view on all default themes, from which you can almost generate the preview of nearly all combinations of the four components listed above. However, it seems the web site does not support the auto-generation of the theme source code directly, you still need further labour job to create the theme yourself.

The following are some specific tips for editing beamer, you can either put them in the preamble of your ".tex" file, or compact them in your ".sty" file for theme definition:



+ To insert page number:
\setbeamertemplate{footline}[page number] 

+ To delete the navigation bar (those short-cuts functioning forward, backward, etc.)
\setbeamertemplate{navigation symbols}{}

+ To set a shading effect for background
  • \beamertemplateshadingbackground{bottom_color}{top_color}
  • \setbeamertemplate{background canvas}[vertical shading][bottom=bottom_color, middle=middle_colour, top=top_color]
+ To put include the title in a block on the title page
\setbeamercolor*{title}{use=structure,fg=white,bg=structure.fg!10!black}
\setbeamertemplate{title page}[default][colsep=-4bp,rounded=true,shadow=true]

+ To set round and shadow features for block
\setbeamertemplate{blocks}[rounded][shadow]

+ To separate frame title with content (add a sub-frame for title)
\setbeamercolor{frametitle}{bg=black, fg=white}

+ To number sections and subsections in table of contents
\setbeamertemplate{sections/subsections in toc}[option]
"Option" could be ball or square or circle or triangle. These options can be applied for the itemize feature as well.

+ To disable subsection description in the predefined miniframe
  • \useoutertheme[subsection=false]{miniframes}
  • \setbeamertemplate{headline}
    {
    \begin{beamercolorbox}{section in head/foot}
    \vskip2pt\insertnavigation{\paperwidth}\vskip2pt
    \end{beamercolorbox}
    }

2012年11月6日星期二

Feel Bored of LaTex Beamer Themes?

As it's known, Beamer is a very nicely-designed LaTex document class assisting presentations. There are a number of predefined Beamer themes available after Beamer is successfully installed in your machine. For Mac users who are using TeXShop, all beamer theme packages are stored under the following directory:

/usr/local/texlive/2009/texmf-dist/tex/latex/beamer/themes/theme/

Look at Beamer Theme Gallery for a structured index of all default themes.

However, we are never easy to be satisfied. The more you use Beamer, the more you would like to explore new features, or new templates. Do you ever feel bored of the predefined LaTex Beamer themes (usually mixed with the unnatural blue color)? 

Of course, if you are an expert on LaTex and Beamer, you can design really fancy template of your own. But most users are just "pure users" (including myself), it means we regard that to develop new packages or new templates is the job of real geeks. We just want to use the already existing stuff or do some really tiny modifications.

Here are two different Beamer templates (the black-white color, which gives you a more "pro" look) one could borrow:
  1. Keynote style Beamer template
  2. Manhattan Beamer theme
One can update the above templates with his own preferences, such as adding the page numbers, deleting the frame split, etc. Notice that the beamer theme files are also ".sty" files. To use them is nothing different from installing/adding other packages. Which means, either copy the ".sty" file under the same directory as the ".tex" file; or put it under 

/Users/your_name/Library/texmf/tex/latex

A small remark on the name of the theme file: all theme files seem to start with the prefix "beamertheme", for example, "beamerthemeFrankfurt.sty", or "beamerthemeManhattan.sty". This format should be strictly satisfied to ensure the successful importing of a corresponding template.

Install New Packages in TeXShop for Mac

If you are using Mac, if you are using TeXShop for editing LaTex, if you want to install/add new packages to your LaTex library, then the following information might be useful.

The trick is actually to include the new package file (normally the ".sty" file) in the search path of your editor. There are two usual ways:

  1. If you might only use the package file once or not many times, or if you would not be bothered by copying the package file everywhere, the most straight forward solution is to keep the ".sty" file under the same directory as your ".tex" file. But as mentioned above, if you are creating a series of files, such as lecture slides, you have to copy the ".sty" under every directory.
  2. Another more efficient and "once for all" solution is to add the ".sty" file, namely the package under the system default directory, where the editor will check every time when it compiles the ".tex" file. For Mac users who are using TeXShop, the searching path is: "/Users/your_name/Library/texmf/tex/latex", replace "your_name" with the login of your computer. Notice that you can create sub-directories under the above directory.
In fact, the above directory is just one of severals that will be searched, there is a more deeply embedded one under "/usr/local/texlive". However, modifying structures there might cause some unknown problem later on. So it is suggested to just use the directory mentioned above in the second method.


Indentation Cancellation

I myself is a great fan of LaTex formatting tool, not only for scientific papers and presentations, but also for casual writings. I feel LaTex is the only tool that free me from looking at format and content at the same time. I became really "lazy" after using LaTex, so "lazy" that I even forgot many formatting command in MS Word.

This series of articles aims to provide some practical tips for LaTex, most of which is the one I encountered during my own experience. I am sorry that I am not able to quote all the references, since most tips were cumulated from all sorts of technical forums on Internet, and  they were recorded by me long time ago, but I will try my best to keep the reference if possible.

No indentation in articles - LaTex automatically provides indentations for an article. The mechanism is a little bit wield because for some paragraph you might find indentation, for some else, there is nothing. This property seems rather absurd for a beginner. If you do insist using no indentation, here is the command you need to put in the document declaration part: