Tuesday, March 30, 2010

[QnA] Use the latest version of exam.cls for preparing exam papers

When preparing an exam paper, we might want to have two versions of this exam paper: one with answers for teachers and the other has only questions for students.

The exam.cls makes this task easy. But make sure you are using the latest version of it or you will not have the handy ``ifprintanswer'' function.

Here is an sample to show how to use the exam.cls with the ifprintanswer function:

\documentclass{exam} % Set ``exam'' as the document class
\begin{document}
\newcommand{\ans}[1]{\ifprintanswers{#1}\fi} % define a new command to simplify
% the following usage

\printanswers % Add this line to print the answers.
% If no answers printing is wanted, just comment this line out.

\begin{questions}
\question
\ans{The answer} The question's description
\begin{choices}
\choice The choice A
\choice The choice B
\choice The choice C
\choice The choice D
\end{choices}
\end{questions}
\end{document}

---
Ref:
Using the LaTeX exam class

Thursday, March 25, 2010

[QnA] Add cls or sty files for LaTeX

We always use certain packages in the latex files, and sometimes the packages are just not there so we need to add them for further working. It's was a big problem for me when I knew very few about the working principle of LaTeX. Now I think I have got a simple and clear process to add packages, that is, *.cls or *.sty, for LaTeX. Here is an example in which I added a so-called exam package for preparing exam papers.

  • Step 1. Search for the exam package and download it from CTAN, and then of course unzip it.
  • Step 2. Make a directory for adding the exam package. In my Ubuntu system, it is:
/usr/share/texmf-texlive/tex/latex/exam/
  • Step 3. Copy all the files of the downloaded and unzipped exam package to the directory we just made.
  • Step 4. Run the texhash command as root. Done.

Monday, March 22, 2010

[SW] Use ps2pdf to convert EPS to PDF

Sometimes we need to convert the eps files to the pdf format. To utilize ps2pdf is a straightforward choice. However, it's not easy, at least for me, to find out how to use some options. Fortunately, I found one article mentioned about how to fit the eps figure to the output pdf:

EPS to PDF : how to avoid clipping

Just to use the ``-dEPSFitPage'' option. More options can be found at the following site:

How to use Ghostscript

---
(I tried the option ``-dOREIENT1=true/false'' but has no expected results. I cannot find related solution. There is a bug report that followed no further response: ``[Bug-gs] Lanscape view instead of Portrait of a pdf file'')

Thursday, March 18, 2010

[SW] Plot the CSV files using Gnuplot

I have used gnuplot for data plotting for several years, but there has been one minor thing bothering me until today.

With my previous (not correct) understanding, gnuplot can only handle data files separated by spaces. Therefore, whenever I wanted to visualize the csv files with gnuplot, I had to use awk or sed to remove the commas and then save the results into new files. It made my folders contain redundant data files which were just for using gnuplot. It's a little stupid, I always thought.

Today I encountered several csv files again, but this time I made up my mind to find the solution out. From the searching results with Google, I found there have been some people asking the same question but without easy solutions given. Then I found a document of old version gnuplot. It gives the right approach: to set datafile separator.

So, next time when you need to handle the csv files with gnuplot, remember to add the following line in the gnuplot script:
set datafile separator ","

Tuesday, March 16, 2010

[SW] Tips for Gnuplot -- Multiplot with EPS output setting

It's very often to show several figures in a single plot. With ``set multiplot,'' gunplot can do this work pretty good. There will be some problems, however, when we don't give proper settings for gunplot. One of the problems I've met was the gnuplot did not print out complete plot containing multiple figures with the terminal set as postscript.

I tried and searched for the solution, and finally got the key. It is better to set the size for the entire plot before the multiplot setting.

Here is my original WRONG setting structure:
  1. set the terminal
  2. set the output filename
  3. set the size for individual figures
  4. set multiplot
  5. set the origin for individual figures
  6. plot individual figures
  7. unset multiplot
And the following is the corrected one:
  1. set the terminal
  2. set the output filename
  3. set the size for the entire plot
  4. set the origin for the entire plot
  5. set multiplot
  6. set the size for individual figures
  7. set the origin for individual figures
  8. plot individual figures
  9. unset multiplot
With the wrong setting structure, we can still get normal output with some terminals such as png. If what we want is something like eps files, then it's necessary to use the right setting structure, which actually makes better sense.

---
Let me show you the (correct) example.
1 set terminal postscript eps enhanced color solid 20
2 set output "test_nogood.eps"
3
4 set nokey
5 set size 1,1
6 set origin 0,0
7
8 set multiplot
9
10 set tmargin 0.5
11 set bmargin 0.5
12 set rmargin 1.0
13 set lmargin 5.0
14
15 set xrange [-25:25]
16 set ylabel "f(x)"
17 set noxtics
18 set noytics
19
20 set size 1,0.20
21 set origin 0.0,0.76
22 set title "sin(x)" offset 0,-1.0
23 plot sin(x) w l lt 1 lw 2
24 set size 1,0.20
25 set origin 0.0,0.54
26 set title "cos(x)" offset 0,-1.0
27 plot cos(x) w l lt 9 lw 2
28 set size 1,0.20
29 set origin 0.0,0.32
30 set title "sin(x)/x" offset 0,-1.0
31 plot sin(x)/x w l lt 3 lw 2
32 set size 1,0.20
33 set origin 0.0,0.10
34 set xlabel "x"
35 set title "x*sin(x)" offset 0,-1.0
36 plot x*sin(x) w l lt 8 lw 2
37
38 unset multiplot
39
40 reset
41

You will get the following plot:
If you change line 5 as ``set size 1,0.20'' and comment out lines 6, 20, 24, 28, and 32, the output result will be as the following plot:
It gives only the last part of the eps file.

---
References:
Gnuplot FAQ: 7.6 My output files are incomplete!
Visualize your data with gnuplot

Friday, March 12, 2010

[SW] Convert XLS to CSV

I got a bundle of xls files and want to extract their data for further analysis. The first thought occurred to me was to find some command line tools to help me convert all the xls files into some kind of text files. Then I found xls2csv, which can be got via apt-get by installing catdoc. So what you need is typing
$ sudo apt-get install catdoc
in your Ubuntu terminal.

The catdoc is useful when you want to take a look at the textual information in doc files.

Friday, March 05, 2010

[Py] Save the figure before show it

This might be a simple and trivial tips, but I didn't know it till today. I occasionally got a blank figure saved by matplotlib's savefig function and had no idea about that strange result. Then I decided to test it with simple Python code.

My test code:
--------------------------------------------
1 from pylab import *
2

3
y = []
4
for x in range(0,10):

5
y.append(x*x)

6

7
plot(range(0,10), y)

8
savefig('fig_saved.png')

9
show()

--------------------------------------------

After running the above code, you should get the foll0wing figure saved by using savefig() function.But if you exchange line 8 and line 9, you get nothing but a totally blank figure.