One Liner Tips & Tricks

This page is a collection of various tips and tricks that fit into one line (more or less). Theres no specific topic - just a collection of things that needed to be solved at one time or the other
Topics: | Multimedia | System Administration | Tex / LaTeX | R | Vim | MOE |
Multimedia
1. How can I resize an animated GIF?
2. How can I play MP3's using rhythmbox on Fedora Core 3?
3. How can I play MP3's with Amarok?
4. How do I install gstreamer and associated plugins/frontends on Fedora Core 3?
5. How do I enable Real support in MPlayer using the LiveMedia libraries?
6. How can I save Real streams?
7. How can I play Real streams without buffering pauses?
8. How can I combine several movies into a single AVI file?
9. How can I rip a DVD to DivX?
10. How can I convert a WMA file to WAV?
System Administration
11. How can I load data in a PostgreSQL DB using COPY and not be root?
12. Setting up Bugzilla and PostgreSQL
13. How do I send STDOUT and STDERR to seperate files?
14. How do I convert a SVN repository from one type to another?
15. How do I view older versions of a binary file in an SVN repository?
16. How can I set up a PDF printer?
17. How do I get Vmware
18. How do I setup passwordless logins using SSH?
19. How do I set up stunnel to access SSL enabled services?
20. How do I get a list of installed packages on Gentoo
21. How can I get rid of ad's during web surfing?
22. How do I add a user to a MySQL system?
Tex / LaTeX
23. Source code coloring?
24. How do I flow text around a figure?
25. How do I get round the pound bug in dvips?
26. How can I get sharper images in PDF files generated using LaTeX?
27. How can I get ticks and crosses in LaTeX?
28. Any ideas about posters in LaTeX?
29. How can I have two lines in a subscript?
30. How do I make a mathematical vector like ___\ rather than ---> ?
31. How do I prevent a paragraph from being indented?
32. How can I get an arial font under LaTeX?
33. How can I insert margin notes in a document?
34. How can I get thick and thin horizontal lines in a longtable?
35. Is there any way to query CTAN from a script to get package listings, recent packages?
36. When I use epstopdf to convert an EPS file to PDF for use in pdflatex the final graphic is rotated.
R
37. How can I use a mathematical expression that will contain the value of a variable in the title/axes of a plot?
38. How can I access SOAP based web services in R?
39. How can I use subscripts/superscripts in a plot text expression?
40. Installing SJava with gcc 4.0
Vim
41. How do I make vim show tabs explicitly?
42. How can I insert text at the beginning and end of a visual selection?
MOE
43. How do I export the structures from a MOE database to SDF?
44. How do I export certain fields from an MDB file to a plain text file?

Multimedia
How can I resize an animated GIF?
Imagmagick will allow you to do this. First split the original animation into the individual frames by doing
 convert anim.gif frames%02d.gif 
Then resize the frames while recombining them as
 convert frames* -delay 200 -coalesce -resize 500x500 newanim.gif
Here the value of delay indicates 2 sec delay between frames. The -coalesce argument is required to so that frames (which are generally of different sizes) are combined properly, rather than mispositioned
Top
How can I play MP3's using rhythmbox on Fedora Core 3?
To get rhythmbox to play MP3's on Fedora Core 3 you will need gstreamer-plugins-mp3. However for this to install it requires a number of packages. These can be obtained from rpm.livna.org and freshrpms.net
  • libid3tag
  • libmad
  • lame
Another aspect is that (at least on my system) rhythmbox will consistently crash when an MP3 is started giving the error:
rhythmbox: pcm.c:2094: snd_pcm_wait: Assertion `err == 1' failed.
This bug has been noted here. One way t get around this is to start gstreamer-properties from the command line and change the default sink from ALSA to something else. I used OSS and now rhythmbox plays fine.
Top
How can I play MP3's with Amarok?
The proces is similar to the above. You'll need lame and taglib which you can get from the livna repository. In case of Fedora Core 4 all you need to do is to add the livna repository to your yum configuration. This can be done by
 rpm -ivh http://rpm.livna.org/fedora/4/i386/RPMS.lvn/livna-release-4-0.lvn.5.4.noarch.rpm
 wget http://rpm.livna.org/RPM-LIVNA-GPG-KEY-i386
 rpm --import RPM-LIVNA-GPG-KEY-i386
 
Once you've done this you can get the gstreamer MP3 plugin by doing
 yum install gstreamer-plugins-mp3
 
For Fedora Core 3, since livna is not a standalone repository it has to be used in conjunction with Fedora Extras a described here
Top
How do I install gstreamer and associated plugins/frontends on Fedora Core 3?
The easiest way is to set up yum to get everything from the gstreamer repository. The instructions for this are given here
Top
How do I enable Real support in MPlayer using the LiveMedia libraries?
To play RTSP streams (Real streams) MPlayer must be compiled with the LiveMedia libraries. This page describes how to compile MPlayer with the libraries.

However when I tried to compile MPlayer with the October and September builds of the LiveMedia libraries, I would get an error regarding a protected class in UsageEnvironment.hh. To fix this go to the LiveMedia source direcory and open up UsageEnvironment/include/UsageEnvironment.hh and comment out line 77. Recompile the LiveMedia libraries and then recompile MPlayer and it works fine.

Top
How can I save Real streams?
Use the dumpstream option of mplayer as: mplayer -dumpstream rtsp://path_to_stream. The stream can then be converted to wav format by doing: mplayer -ao pcm stream.dump

The output file is audiodump.wav which can then be converted to MP3 by doing : lame -m s -q 2 --vbr-new -V 1 -B 320 audiodump.wav song.mp3

(NOTE that the MP3 encoding command line does not include tags)
Top
How can I play Real streams without buffering pauses?
To play Real streams without buffering pauses: mplayer -dumpstream -dumpfile xxx.rm rtsp://where/the/stream/is and then wait 5 minutes and from another console do: mplayer xxx.rm (Of course you loose the immediacy, 5 minutes worth at least)
Top
How can I combine several movies into a single AVI file?
cat 1.avi 2.avi | mencoder -noidx -ovc copy -oac copy -o output.avi -
Top
How can I rip a DVD to DivX?
How can I convert a WMA file to WAV?
mplayer file.wma -ao pcm -aofile file.wav
Top
System Administration
How can I load data in a PostgreSQL DB using COPY and not be root?
At the beginning of your data file add the line
 copy table_name from stdin with delimiter as '\t' null as 'null;
 
and at the end of your data file add
 \.
 
 
The simply do
 psql -U username -W -d dbname < datafile.txt
Top
Setting up Bugzilla and PostgreSQL
When configuring PostgreSQL as described here make sure that the other lines in pg_hba.conf are commented out (or at least exclude the bugs user)
Top
How do I send STDOUT and STDERR to seperate files?
In case of the bash shell you can use the following
 ~: (command   3>&1 >stdout.log 2>&3  3>&-) >stderr.log 
Top
How do I convert a SVN repository from one type to another?
 # svnadmin create --fs-type fsfs /path/to/newrepos
 # svnadmin dump /path/to/oldrepos | svnadmin load /path/to/newrepos 
See here for more details
Top
How do I view older versions of a binary file in an SVN repository?
This situation can arise if you use SVN to version control binary documents (Word documents for example). In this case there are two ways we can take a look at a previous revision.

Say your current revision of text.doc is 38 and you want to take a look at revision 37. This is achieved by doing

 svn cat -r 37 text.doc > oldversion.doc
Once you're done with oldversion.doc you can get rid of it.

An alternative route, which is more applicable if you want to undo previous changes is described here. In short, to go back to revision 37 we would do

 svn merge -r 38:37 text.doc
Thus your current copy (i.e., the copy in the current working directory) is now revision 37. However the repository still contains revision 38. So if you decide you really want to keep revision 38 simply do
 svn revert text.doc
and your working copy will be updated to the latest revision in the repository.

For simply viewing older versions of a binary doc, the first approach is much easier.

Top
How can I set up a PDF printer?
If you're using CUPS then download the CUPS-PDF package from here . After it's installed restart CUPS and point your browser to http://localhost:631 and click on 'Manage Printers' and then click on 'Add Printers'. Give a name and description and then select the 'Virtual PDF' device and then for make and model select the Postscript and Color Postscript options respectively. Now applications will see the printer you named above and if you print to it you should get a PDF file under /var/spool/cups-pdf/USERNAME/ where USERNAME is your user name on the system.

This is useful if you want to convert documents (say from OpenOffice ) to PDF format without starting the OpenOffice GUI. Simply do

  ooffice -pt "PDFWriter" stuff.doc
  
Here "PDFWriter" is the name of the PDF printer you have set up via CUPS. In fact the above commandline will take any file format the Ooffice recognizes and dumps PDF formatted versions in the location mentioned above.
Top
How do I get Vmware
First install the Vmware RPM (or tarball) and then open up vmware-config.pl and look for the section:
  $header_page_offset = direct_command(
       shell_string($gHelper{'echo'}) . ' '
       . shell_string('#define __KERNEL__' . "\n" . '#include
  <
  asm/page.h
  >
  '
                      . "\n" . $pattern . ' __PAGE_OFFSET') . ' | '
       . shell_string($gHelper{'gcc'}) . ' ' . shell_string('-I' . $answer)
       . ' -E - | ' . shell_string($gHelper{'grep'}) . ' '
       . shell_string($pattern));
     chomp($header_page_offset);
     $header_page_offset =~ s/^$pattern \(0x([0-9a-fA-F]{8}).*$/$1/;
     if ($header_page_offset =~ /[0-9a-fA-F]{8}/) {
       # We found a valid page offset
       if (not (lc($header_page_offset) eq lc($gSystem{'page_offset'}))) {
         if ($source eq 'user') {
           print wrap('The kernel defined by this directory of header files
           does ' . 'not have the same address space size as your
           running ' . 'kernel.' . "\n\n", 0);
         }
         return '';
       }
     }
  
and comment the final return statement. Once you have that download the any.any update from here and untar it. Go into the directory and run runme.pl and follow instructions.
Top
How do I setup passwordless logins using SSH?
  1. On A, 'ssh-keygen -t rsa' and press enter for everythin
  2. On A, 'scp ~/.ssh/id_rsa.pub B:/home/username/'
  3. ssh to B (you'll be asked for your password)
  4. On B, 'ssh-keygen -t rsa' and press enter for everything
  5. On B, 'cat id_rsa.pub > > ~/.ssh/authorized_keys2'
  6. On B, 'scp ~/.ssh/id_rsa.pub A:/home/username'
  7. Come back to A and do 'cat id_rsa.pub > > ~/.ssh/authorized_keys2'
After all this is done you should be able to ssh to B from A (and to A from B) without being asked for a password
Top
How do I set up stunnel to access SSL enabled services?
If you need to connect to a POP/NNTP/IMAP server which is SSL enabled but your client does not support SSL (eg. pan) you'll need to setup stunnel.

After having installed stunnel you can create your own SSL certificates (here or here) but I went along with whatever came in the tarball. You'll need to edit /etc/stunnel/stunnel.conf or else copy stunnel.conf.sample that came with the tarball to this location. If you created your own certificates you should uncomment the related lines. My stunnel.conf file looks like this:

 cert = /etc/stunnel/stunnel.pem
 chroot = /var/run/stunnel/
 pid = /stunnel.pid
 setuid = nobody
 setgid = nobody
 debug = 7
 output = stunnel.log
 client = yes
 foreground = no
 [nntps]
 accept = localhost:119
 connect = news.psu.edu:563
 
This is setup so my NNTP client (pan) can acess my university NNTP server which is SSL enabled. Make sure that /var/run/stunnel exists and is writable by the user and group that stunnel will run under (in this case nobody). Once this is all done, as root do /usr/sbin/stunnel and it should start running. I start it from /etc/rc.d/rc.local so that it starts at bootup.

Finally, start up pan and in the server configuration, rather than specifying the real NNTP server, use localhost as the host and 119 for the port and you should be able to access the news server

Top
How do I get a list of installed packages on Gentoo
find /var/db/pkg/ -mindepth 2 -maxdepth 2 -printf "%P\n"

find /var/db/pkg/ -mindepth 2 -maxdepth 2 -printf "%P\n" | grep < your package >

Top
How can I get rid of ad's during web surfing?
The first thing is to get Mozilla or Firefox . There are multiple ways ads can be blocked.
  • Get a list of hosts that serve ads and make it your /etc/hosts where each hostname is set to 127.0.0.1. You can get such a list here
  • Use a stylesheet which blocks various HTML elements responsible for showing ads. You can get one such CSS file here . Its for Galeon but will probably work for Mozilla and Firefox .
  • Get the Adblock plugin.
Top
How do I add a user to a MySQL system?
to allow a user foo to connect to a database bar with password eek start MySQL as root and then do

grant all privileges on bar.* to foo@localhost identified by 'eek' with grant option;
Top
Tex / LaTeX
Source code coloring?
Use the listings package. Also see here for a good collection of colorings and style definitions for some languages
Top
How do I flow text around a figure?
There are a number of packages that can do this (floatflt, picins and wrapfig). I use wrapfig and an example is
 \begin{wrapfigure}{l}{.48\linewidth}
     \begin{center}
         \includegraphics[width=\linewidth]{picturefilename}
         \caption{Some caption}
         \label{fig:somelabel}
     \end{center}
 \end{wrapfigure}
Top
How do I get round the pound bug in dvips?
The 'pound' bug in dvips: When converting a DVI file to a PDF file you need to use dvips to generate a PS file first. However, if you use

 dvips -Ppdf file.dvi -o file.ps
to generate the PS file, anywhere the string fi occurs in the original TeX file it will be converted into a pound symbol in the final PDF. To get around this append the -G0 flag to the above dvips command line
Top
How can I get sharper images in PDF files generated using LaTeX?
Sharper images in PDF files using LaTeX: When using ps2pdf to convert a Postscript file to PDF, eps images in the original document are downsampled. Thus they look fuzzy in the final document (and crappy when you view the page full screen). To get around this add

 -dPDFSETTINGS=/prepress -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode
to the psp2df command line
Top
How can I get ticks and crosses in LaTeX?
To get ticks or crosses use the Zapf Dingbats font.
 \usepackage{pifont}
 \ding{51} %% tick 
 \ding{55} %% cross
Top
Any ideas about posters in LaTeX?
How can I have two lines in a subscript?
 \usepackage{amsmath}
 
 \begin{equation}
   x_{\substack{a\\b}}
 \end{equation}
  
Also see The short math guide
Top
How do I make a mathematical vector like ___\ rather than ---> ?
 \usepackage{amsmath}
 \makeatletter
   \def\rightharpoonupfill@{\arrowfill@\relbar\relbar\rightharpoonup}
   \newcommand{\overrightharpoonup}{%
   \mathpalette{\overarrow@\rightharpoonupfill@}}
 \makeatother 
and then use \overrightharpoonup{abcd}. Alternatively
 \documentclass{article}
 \usepackage{esvect}
 \begin{document}
   $\vv{abcd}$
 \end{document} 
Top
How do I prevent a paragraph from being indented?
To prevent a single paragraph from being indented, place the command \noindent before it. To prevent indentation of all paragraphs set \parindent to 0 by doing: \setlength{\parindent}{0em}
Top
How can I get an arial font under LaTeX?
The closest you can get is Adobe Helvetica and can be had by doing:
 
 \renewcommand{\rmdefault}{phv}\renewcommand{\sfdefault}{phv}

Alternatively,

 \usepackage{helvet} \renewcommand{\familydefault}{\sfdefault} 
Top
How can I insert margin notes in a document?
See here. The macro that does it is reproduced here:
  \newcommand{\query}[1]{\marginpar{%
      \vskip-\baselineskip
      \raggedright\footnotesize
      \itshape\hrule\smallskip#1\par\smallskip\hrule}} 
So an example would be
      Do some stuff with the code and go 
      for dinner and come back\query{Should we come back?}
      later and finish it off 
If you're using the \emph{setspace} package then you probably want the margin notes to be single spaced, whatever the spacing of the document as a whole. In that case the firt line above would look like
 \newcommand{\query}[1]{\marginpar{\singlespacing% 
Top
How can I get thick and thin horizontal lines in a longtable?
By default, horizontal rules in a longtable are generated using \hrule. However you might want the horizontal rules to be created like in a ctable (first line at the head of the table is thick and all subsequent lines thin and the footer line is again thick - all with proper spacing).

The solution is to use the booktabs package and then use \toprule, \midrule and \botomrule (equivalent to \FL, \ML and \LL from the ctable package). A snippet that shows how to get horizontal rules in a longtable look like those in a ctable

 \usepackage{longtable,booktabs}
 ...
 \begin{longtable}{lrr}
     \caption[A Caption]{A Caption}\label{lab} \\
     \toprule
     Head 1 & Head 2 & Head 3 \\
     \midrule
     \endfirsthead
     \caption[]{(continued)} \\
     \toprule
     Head 1 & Head 2 & Head 3 \\
     \midrule
     \endhead
     \bottomrule
     \endfoot
     %% Put the rows of your table here
 \end{longtable}
Top
Is there any way to query CTAN from a script to get package listings, recent packages?
FTP to ftp://ftp.tex.ac.uk/tex-archive/ and check out FILES.bydate, FILES.byname, FILES.last07days
Top
When I use epstopdf to convert an EPS file to PDF for use in pdflatex the final graphic is rotated.
The solution to this is to open up epstopdf in an editor and add a line. Around line 195 you should see the following code
 ### open output file
 if ($::opt_gs) {
   my $pipe = "$GS -q -sDEVICE=pdfwrite $GSOPTS " .
           "-sOutputFile=$OutputFilename - -c quit";
   debug "Ghostscript pipe:", $pipe;
   open(OUT,"|$pipe") or error "Cannot open Ghostscript for piped input";
 }
Replace this with
 ### open output file
 if ($::opt_gs) {
   my $pipe = "$GS -q -sDEVICE=pdfwrite $GSOPTS " .
 	  "-dAutoRotatePages=/None " .
           "-sOutputFile=$OutputFilename - -c quit";
   debug "Ghostscript pipe:", $pipe;
   open(OUT,"|$pipe") or error "Cannot open Ghostscript for piped input";
 }
Top
R
How can I use a mathematical expression that will contain the value of a variable in the title/axes of a plot?
 plot(1:10,1:10, main=substitute( R^{2} == var, list(var=var)))
Top
How can I access SOAP based web services in R?
First install the SSOAP, RCurl and XML packages. The next step is to have a web service and methods to call. For this example we use the ToxTree web service.
 library(SSOAP)
 s <- SOAPServer('http://blue.chem.psu.edu','/tox/services/toxTreeWS', 8080)
 ret <- .SOAP(s, 'getCramerClass', smiles='CCN', action="urn:xmethods-getCramerClass')
 
The return value of this web services' method is a string which has two parts seperated by a '#'. You can use strsplit to get at the individual components
Top
How can I use subscripts/superscripts in a plot text expression?
 plot(x,y, xlab='HN Output', ylab=expression(paste('Observed ', log,' (',K[p],')'))
Top
Installing SJava with gcc 4.0
Recently I had tried installing SJava on my Fedora Core 4 system which has GCC 4.0. The install process reported the following error
 CtoJava.cweb:215: error: static declaration of 'std_env' follows non-static declaration
 CtoJava.cweb:195: error: previous declaration of 'std_env' was here 
The simple way to fix this is to untar the SJava package resulting in an SJava directory. Then open up SJava/src/RSJava/CtoJava.h and line 14 will read as
 extern JNIEnv *std_env;  /* initialized in create_Java_vm */ 
Change this to
 static JNIEnv *std_env;  /* initialized in create_Java_vm */ 
After patching the source I also had to edit the configure file since it was not finding my R shared library. The diff between my version and the original version is
 2809c2809
 < if test -f ${R_HOME}/lib/libR.so ; then
 ---
 > if test -f ${R_HOME}/bin/libR.so ; then
 2811c2811
 <   R_SHARED_LIB="-L${R_HOME}/lib -lR"
 ---
 >   R_SHARED_LIB="-L${R_HOME}/bin -lR"
In addition if you are trying to get SJava running on an AMD64 system you will need to add -fPIC to Makefile_rules.in in the SJava package. (Thanks to Egon Willighagen for pointing this out).

After all the edits are done go to the directory containing the SJava sources and then from the command line do

 #: R CMD build SJava 
 #: R CMD INSTALL -c SJava_0.68.tar.gz 

After installation I had to make two other changes. In my ~/.bash_profile I added

 LD_LIBRARY_PATH=/usr/local/lib/R/library/SJava/libs:\ 
 /usr/local/share/java/jdk1.5.0/jre/lib/i386/client:\
 /usr/local/share/java/jdk1.5.0/jre/lib/i386:\
 /usr/local/share/java/jdk1.5.0/jre/../lib/i386:$LD_LIBRARY_PATH
Also in /etc/ld.so.conf I added the line
 /usr/local/lib/R/lib
(I could have put this line in my ~/.bash_profile).
Top
Vim
How do I make vim show tabs explicitly?
You can use ":set list" (which defaults to "nolist"), which shows tabs as ^I and puts a "$" at the end of lines. You can also define highlighting regions with "syn match" to define a single tab. You can then link this to whatever highlighting you like perhaps using the following:
 :syn match TAB_CHAR "\t"
 :hi link TAB_CHAR Error
(Thanks to Tom Chase)
Top
How can I insert text at the beginning and end of a visual selection?
To wrap a visually selected piece of the text by say, a \mathbf{} tag, use
vmap ;bf "xc\mathbf{}<esc>"xP
Note that this doesn't appear to work if you have the LaTeX-VIM package loaded
Top
MOE
How do I export the structures from a MOE database to SDF?
Assuming the database is called db.mdb and the structures are the column title mol we can do
db_ExportSD[ 'db.mdb', 'structures.sdf', ['mol'], [] ]
This exports all the structures to the file called structures.sdf
Top
How do I export certain fields from an MDB file to a plain text file?
Assuming you have a MDB file and you want to dump the contents of a column containing MACSS fingerprints. Let the column be called FP:MACCS. Then we can do
 entry_keys = db_Entries 'somedb.mdb'
 db_ExportASCII[ 'somedb.mdb', 'fp.txt', ['FP:MACCS'], entry_keys ]
Here somedb.mdb is the name of the MOE database that you want to extract from

In case you want to extract a set of fields from a MOE DB then use the following

 entry_keys = db_Entries 'somedb.mdb'
 fields = dbv_Fields 'somedb.db'
 mask = m_id igen length fields
 mask[ [1,2,3] ] = 0
 db_ExportASCII[ 'somedb.mdb', 'vars.txt', fields|mask, entry_keys ]
Here we specify that the first three fields are not to be exported.
Top