Please see more tips in the TeXnical Notes.

## Computing

### Matlab

1. Could you say what is the result of the following operation: 1e15 * sum([-10, 1e-15, 10]) ? If you gave answer = 1, put the above expression in MATLAB. You will get approximately 1.78. So, it is almost an error of 80%. To avoid that possible blunder for ill-conditioned vectors, always sort you data by absolute value in a descending order before computing a sum. For example, 1e15 * sum([-10, 10, 1e-15]) gives you unity as expected. Another instructive example is if ( 12345678901234567^3 + 2^3 == 12345678901234568^3 )
disp('be careful with floating-point arithmetic')
end
2. Changing KDE Windows' Properties to lock all MATLAB windows to one desktop (Desktop 5 in this example): Right-mouse click on the top-left window Configure Window Behaviour.... Then Window-Specific Settings -> [New...] -> [Detect]. Click on any MATLAB window, tick "Whole window class". Finally [Geometry] -> Desktop -> Force -> Desktop 5 . E.g., my settings are (Default) Disable focus stealing prevention for MATLAB Windows class; sun-awt-x11-xframepeer com-mathworks-util-postvminit [exact match] [Geometry] -> Desktop -> Force , 5 [Workarounds] -> Allows focus stealing prevention
3. --------------------------------
DISTRIBUTED MULTI-CORE COMPUTING
--------------------------------
For MATLAB newer than 7.8.* (2009), the multithreading (using more than one CPU core) is set up automatically. To check the number of processor cores available in a PC/server, use feature('NumCores') Note, that this is an undocumented function (see more details). And the command memory gives you the system memory volume/usage information (MS Windows only).
4. Use parfor from the Parallel Computing Toolbox to split the for-cycle between several MATLAB 'workers'. For example, to start a parallel computing session with a maximum of 2 'working' MATLAB instances (plus one coordinating MATLAB client), use matlabpool open local 2
...
parfor i = (1 : N)
B(i) = function(A(i))
end
...
matlabpool close
(more info). This is a perfect technique for Monte-Carlo ensemble averages. See this figure for the performance speed-up on a busy 12-core Intel Xeon cluster ('chiron').
5. To override the default maximum usable number of CPU cores on the cluster, use the NumWorkers property. In the example below, we set the maximum to 12 cores (upper limit in MATLAB) and open a parallel session with 4 'workers' NUM_CORES = 4; myCluster = parcluster('local'); myCluster.NumWorkers = 12; saveProfile(myCluster); matlabpool('open', 'local', NUM_CORES);
6. ----------------------
GRAPHICS AND EXPORTING
----------------------
Note that the scaling of an .eps-figure depends on the screen resolution you set before launching Matlab. A running Matlab session will not adjust automatically when you alter the screen settings. For example, a reduced screen resolution will give you a figure with a larger bounding box (effectively reducing the font size).
7. To save the current figure, as it appears on the screen, to the bitmap-format use I = getframe(gcf); imwrite(I.cdata, 'FileName.png');
8. To extract the data from the plot (e.g. if you forgot to save it with 'save()' and the variable is no longer in the active workspace), use hh = get(gca, 'Children');
XX = get(hh,'XData'); % or get(hh(1),'XData') if more
YY = get(hh,'YData'); % than one line was plotted
N.B. the first plotted line goes last in hh.
9. To put a quote character (') in the string (for titles, legends, etc.), use the twice repeated symbol, e.g. 'Newton''s law'
10. To draw a point (x,y) in rgb(0,0,0) on an existing figure, use line('XData',x, 'YData',y, 'LineStyle','none', ...
'Marker','o', 'MarkerSize',7, 'MarkerFaceColor',[0,0,0]);
N.B. x,y can be vectors to draw a set of points.
11. To draw on an earlier figure after some other have been created use hFig = figure('Name','MyFigure');
...
set(0, 'CurrentFigure',hFig); hold on;
12. To plot with multiple axes in a single figure, e.g. with linear scale on the first y-axis and logarithmic scale on the second y-axis, use figure('Name','Double-Axes Example'); hold on; box on;
XX = 1:0.1:10; YY1=sin(XX); YY2=exp(XX);
plot(XX, YY1, '-k', 'LineWidth',2);
hAx1 = gca;
hAx2 = axes('Position',get(hAx1,'Position'),...
'XAxisLocation','top', 'XTick',[],...
'YAxisLocation','right',...
'Color','none','XColor','k','YColor','k',...
'YScale','log'); hold on;
semilogy(XX, YY2,'--k', 'LineWidth',2, 'Parent',hAx2);
xlabel(hAx1, 'X', 'FontSize',16);
ylabel(hAx1, 'Y1', 'FontSize',16);
ylabel(hAx2, 'Y2', 'FontSize',16);
legend(hAx1, {'a'}, 'FontSize',12, 'Location','NorthWest');
legend(hAx2, {'b'}, 'FontSize',12, 'Color','w');
If you want to be able to click-edit the line properties plotted in the first axes, add 'HitTest','off' when creating hAx2.
13. To set parameters of the figure in legend() use {...} for captions, e.g. legend({'caption1','caption2'}, 'FontSize',12);
14. To align the first entry in the figure legend legend({'line^{index}_1', 'line_2'}), which contains subscripts/superscripts, apply [hL,hLL,auxL,auxT] = legend( ... );
hText = findobj(hLL, 'type','text');
set(hText(end), 'VerticalAlignment','Cap');
where 'Cap' aligns with the Capital letter (try also 'Top').
15. To save an .eps figure with a transparent background, use set(gca, 'Color','none');
set(gcf, 'Color','none');
set(gcf,'InvertHardCopy','off');
print -depsc2 Transparent.eps;
16. If you want to display greek letters in uicontrol, which does not support TeX, use the ASCII lookup function chart.m: chart('arial'); % prints out character table
pm_char = char(hex2dec('B1')); % \pm char based on its ascii value
mu_char = char(hex2dec('B5')); % \mu char
uicontrol('Style','text', 'String',[pm_char, ' 10 ', mu_char,'m']);
17. To allow for independent colour maps in sub-plots, use freezeColors and cbfreeze utilities as follows: figure;
hax1 = subplot(2,1, 1);
colormap(hax1, 'jet');
surf(peaks(30)); hc=colorbar;
cbfreeze(hc); % freeze colorbar
freezeColors; % freeze colormap
hax2 = subplot(2,1, 2);
colormap(hax2, 'gray');
surf(10*peaks(100)); colorbar;
Note: cbfreeze and freezeColors are unnecessary for MATLAB 8.4 (2014b) or newer, as long as you specify an axes handle: colormap(h,...).
18. To force scientific (exponential) notation on a vertical axis of the current figure, use exponent = -2;
set(gca, 'YTickLabel', strtrim(cellstr(num2str(get(gca,'YTick')'/10^exponent))') );
fs = get(gca,'fontsize'); xl = xlim; yl = ylim; set(gca,'units','normalized');
text(xl(1),yl(2), sprintf('\\times10^{%d}',exponent), 'fontsize',0.9*fs, ...
'VerticalAlignment','bottom');
(source: example, and another one)
19. -----------
MISCELLANEA
-----------
Starting from MATLAB 2010a, a new option 'index0' will be available for most of standard array-generating function (zeros(), ones(), diag(), sum(), etc.) It forces a so-created array to be indexed from 0 instead of 1. This breaks a nearly three-decades-long tradition of hard-coded indexing from 1, unlike in the majority of programming languages. See more details first-handedly. [Update]: this turned out to be a joke...
20. The second part of 'Callback'-option/value pair is either 'any Matlab code' or @mySubFunction_Callback, the latter WITHOUT quotes (' '). The respective GUI m-file subfunction is @mySubFunction_Callback(hObj, event, eventdata).
21. Be careful with clear Variable; inside the script if the variable under question is global. It is generally safer to set it to zero. Alternatively, use: clear Variable; global Variable;
22. You can operate in bulk with a list of variables (e.g. to empty them), like (LIST_OF_VARIABLES = {'AnArray', 'ACellArray', 'AScalar'};
for i = [1 : length(LIST_OF_VARIABLES)]
Info = whos(LIST_OF_VARIABLES{i});
if strcmp(Info.class, 'cell')
evalc([LIST_OF_VARIABLES{i}, ' = {}']);
else
evalc([LIST_OF_VARIABLES{i}, ' = []']);
end
end
however, it is not a recommended practice.
23. If you want to grow an empty cell array of strings iteratively, do not forget the difference from the ordinary arrays: Numbers = []; new_number=42;
Strings = {}; new_string='abc';
Numbers = [Numbers, new_number];
Strings = {Strings{:}, new_string};
% or even better
Strings = [Strings; {new_string}];
24. To take the last element of an array that satisfies a logical condition, use A( find(A == 3, 1,'last') );
% returns 'ans = 3'
% for A = [1, 2, 3, 3, 3];
25. To convert a matrix to LaTeX with a specified number of significant digits, say 3, use digits(3); A_sym = sym(A, 'd'); latex(A_sym);
26. To run a Matlab script in the detached no-GUI mode on a remote server (say, called 'chiron'), use the screen to emulate a terminal session and matlab -nodisplay to suppress the graphical support by using the following command sequence (in a console): $ssh username@chiron$ screen
$nice matlab -nodisplay -logfile ~/matlablog.txt -r myscript.m ... press <Ctrl>+a, followed by d$ logout chiron
To reconnect to the detached screen session, use $ssh username@chiron$ screen -r
and use exit to close the session.
27. To execute a script or function in command-line and exit, use $matlab -nojvm -nodisplay < my_script.m (in Linux / MacOS), or$ matlab -nojvm -nodisplay -r my_script,quit (in both Linux and MS Windows).
28. Finally, if you are up to a smile or two, try the following: spy % plots a ... sparse matrix

sum * % gives the Answer to the Ultimate Question of Life

### Comsol

1. Unless you have MATLAB installed and COMSOL properly configured to support their communications, you shall save your model in .mph or .fl binary formats; otherwise, you are at risk of not being able to open your own COMSOL .m-file.
2. To run COMSOL in MATLAB environment and 2 COMSOL GUI Desktops (in Linux), use $comsol mphserver matlab &$ comsol &
$comsol & from the same terminal (to avoid checking out multiple licences for the same user). You also need to ensure that the COMSOL configuration file points to the supported version of the MATLAB (e.g. add a command-line option -mlroot /usr/local/matlab... or amend the line in the configuration file: MLROOT=${MLROOT:=/usr/local/matlab...}).
3. To see the current use of network licences, use [in Windows]: All Programs -> COMSOL Multiphysics -> License Tools -> LMTOOLS -> Browse -> C:\Program Files\COMSOL\COMSOL52\Multiphysics\license\license.dat, or [in Linux]: $cd /usr/local/comsol52/multiphysics/license/glnxa64$ ./lmstat -a -c ../license.dat
(source).
4. A Java error or crash at an attempt to show a mesh or at a similar 2d/3d graphical operation in Linux might indicate that you need an OpenGL driver installed. If you cannot find hardware accelerated OpenGL drivers for your graphics card, you should use software OpenGL rendering via 'mesa' libraries provided by COMSOL. In COMSOL 3.5*, use the option -mesa to the start-up script (see also graphics troubleshooting): comsol35a -mesa &
5. To specify a point source use a weak boundary condition: q0 * u_test, where u=u(x,t) is the name of dependent variable in the equation(s). This is equivalent, in 1D-geometry, to setting the boundary coefficient g = q0. In 2D and 3D cases, it is necessary to use Point Settings of a Point object and the only weak formulation for the boundary condition is available.
6. A great care is required when drawing geometrical objects in COMSOL. Check Draw -> Geometric Properties to make sure all points and lines are precisely at the positions they should be. An error of order 1e-4 is easily introduced when drawing manually. A good alternative is to define lines using, e.g. line1 = solid1([0,1]); pt1 = point1(0.25); pt2 = point1(0.5); p.objs = {pt1, pt2}; s.objs = {line1}; fem.draw = struct('p',p,'s',s); fem.geom = geomcsg(fem); from MATLAB, or using COMSOL GUI menu option Draw -> Specify Objects. And do not forget to regenerate/reload the mesh.
7. To set up periodic boundary conditions, go to Physics -> Periodic boundaries... and select the border (edge) that will be a "source" of the boundary data. Note that the corresponding vertices should also be carefully matched in the correct order.
8. For an unknown reason, the analysed geometry with only internal 2D circles/ellipses (ellip2()) and the one with only internal polygons (line2()) has different indexing order for the outer borders. For example, if you subtract a polygon and an ellipse from a rectangular domain ('g_rect2 - g_line2 - g_ellip2') and apply type 1 b.c. on the outer-rectangle, type 2 on the inner-polygon, and type 3 on the inner-ellipse, the final indexing will be as follows: (1,1,1, 2,2,...,2, 1, 3,3,3,3). Be careful and double-check boundary conditions before computing.
9. To run Comsol 4.* in 3.5a-compatibility mode, use comsol server matlab -compat -c35aroot /usr/local32/comsol35a -mlroot /usr/local/matlab7.10
10. To automatically run a series of Studies, e.g. a flow solver, followed by a transport sweep, use the Study Reference: just add the component to Study 2 (transport sweep) and make it refer to Study 1 (flow). Now, running the second study, runs the entire simulation sequence. The Matlab LiveLink scripting is also capable of this or more sophisticated automation. Alternatively, a build-in scripting tool Model Methods from the Application Builder could do the job, but it is only available (as of ver. 5.3a) in the Windows distribution.

### R

1. Help search in R: ?plot # for a command
?spatstat::Kest # for a package
??command # generic search
2. Press <Ctrl> + L to clear the console screen.
3. To reset R session, i.e. to close all connections and clear all workspace variables, use closeAllConnections()
rm(list=ls())
4. Install R on Ubuntu: i. $sudo add-apt-repository 'deb http://cran.ma.imperial.ac.uk/bin/linux/ubuntu xenial/'; ii.$ sudo apt-get update; (if it complains that there is no public key, say, 51716619E084DAB9) ii*. $sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 51716619E084DAB9; iii.$ sudo apt-get install r-base
5. A list of useful GUIs and packages: RStudio - an advanced MATLAB-style programming environment Rcmdr - STATA/SPSS-type GUI for interactive data analysis (see also JGR) (to install R commander, run > install.packages("Rcmdr") from within R and wait!).
6. To install a local package, use $R CMD INSTALL package_name.tar.gz -l$HOME/R-packages/ or install.packages("package_name", lib="$HOME/R-packages/") (more info). 7. To run an R-script, use source('script.R', echo=TRUE) within R, or$R --vanilla < script.R from the command line.

library(package="Hmisc")
describe(data$variable1) # summary statistics ... subset1 <- data[data$var1 == "yes", "var2"] # create a subset
subset2 <- data[data$var2 > 10, "var3"] ... subset3 = subset(data, select=c(var1, var2, id)) # create a subset with ind1 = c("var1", "id"); ind2 = c("var2", "id") # both var1 and var2 subset4 = subset( subset3, # for the same id subset3[ind1]$id == subset3[ind2]$id, select=c(var1,var2) ) subset5 = na.omit(subset4) # drop NA ... mean(subset1, na.rm=TRUE) # compute mean, removing 'NA' values sd(subset1, na.rm=TRUE) # SD quantiles(subset1, na.rm=TRUE) # Quantiles ... plot(subset1, subset2, col = "blue") # scatter plot hist(subset1, col = "blue") # histogram boxplot(data$var1, data$var2, range=0) # box plot 9. Use ggplot2 package with install.packages(“ggplot2”) and library(package = "ggplot2") to access the extensive library of different plotting tools. 10. To write a mathematical TeX-like expression in caption/legend/title, use expression() command instead of a string. E.g. plot( xx, yy, labx="x", laby=expression(y = sqrt(sin(x)^2)), main="..." ) 11. To save a figure as .eps, use postscript(file='figure.eps', width=7, height=7, paper="special", horizontal=FALSE). 12. To set the aspect ratio of x/y axis to 1, increase the font-size and remove all axis-captions (useful for a composite figure), use ratio <- (max(Y) - min(Y)) / (max(X) - min(X)) postscript(file="file.eps', width=7, height=7*ratio, paper="special", horizontal=FALSE) par(mar = c(2,2,1,1)) # trims the margins (bott,left,top,right) par(cex = 1.1) # sets font-size-scale 110% of default plot(X, Y, ..., asp = 1, ann=FALSE ) 13. To invert y-axis, use plot( X, Y, ..., ylim = c(max(Y), min(Y)) ). ## Imaging & Graphics ### XFig - flexible diagrams 1. Installation (Ubuntu) sudo apt-get install xfig gsfonts-x11 2. You can only type in values in a property field when the mouse pointer is over it. 3. When exporting a diagram to .png or other bitmap image, do not forget to select 'some smoothing' or 'more smoothing' option, otherwise you may end up with a fuzzy picture. You could also set an optional width of blank border. 4. When unable to position the objects precisely by moving the points (with the default step of 1/16th inch), switch the Position property (bottom toolbar) to "any" or use the Edit tool to tweak its coordinates arbitrary. 5. A very easy way to write any LaTeX symbols and equations inside XFig and then to get a nice .eps file is to use the fig2eps utility by Marc de Courville and others (please note that this is a modified version with an extra option to handle LaTeX fonts; here is the original fig2eps). The usage is as follows: 1. Insert any text object into XFig and type any formula you like within$ $, set the font size to affect scaling (8-12 would be fine for labeling diagrams). 2. Save the figure, e.g. diagram.fig, and run from the console: fig2eps diagram.fig That's it. The result is a neat figure in the file diagram.eps, ready to be inserted to LaTeX. If you wish to use a mixture of plain text and formulae in the XFig, the option -times is recommended (run fig2eps -h for more details). 6. ------------- InkScape Tips ------------- To crop an imported image (i) insert a rectangle on top of the desired area (ii) Object -> clip -> set (source) 7. To change the default transparent to a white background of the document, go to File -> Document Properties -> Background colour and set Alpha (opacity) to 255. ### Image manipulation with ImageMagick 1. To convert .eps to .png use convert -density DPI -resize WIDTH_PIXELSxHEIGHT_PIXELS -rotate ANGLE file_name.eps file_name.png e.g. convert -density 150 -resize 1024 file_name.eps file_name.png Also, an option -sharpen 0xsigma or -unsharp 0xsigma+amount+threshold (e.g. -sharpen 0x1) can be used to reduce a blur, where \sigma is a parameter of convolution kernel. (source: ImageMagick guru and these notes). 2. To convert a bitmap to PostScript Level 2 (instead of default large-sized Level 1 for convert file.jpg file.eps), either specify format explicitly (convert file.jpg file.eps2) or force the output format: convert -density 150 file.jpg eps2:file.eps You can also try out other raster-to-eps conversion tools, like bmpp or jpeg2ps. 3. To perform a batch conversion of, e.g. .png to .eps, use the following (in Linux command-line): for f in *.png; do convert -density 300$f eps2:${f%.*}.eps; done or FOR %f IN (*.png) DO convert -density 300 %f eps2:%~nf.eps (in MS-DOS shell command-line). 4. Create an animated GIF out of a sequence of images (with 1.2 seconds per frame): convert -delay 120 -loop 0 *.jpg animated.gif See more details 5. Extensive information about any image is given by the command convert FILENAME -verbose info:- and also identify -verbose FILENAME 6. To remove (trim) unnecessary space around an .eps figure produced by MATLAB, use convert figure.eps temp.eps convert -trim +repage -density 150 temp.eps figure_final.eps 7. To crop an image (say, remove 50 px from the top), use convert -crop +0+50 +repage figure.png figure_cropped.png see more info and examples. 8. To convert an .eps-image to .pdf, fitting the output to the bounding box, use GhostScript Distiller ps2pdf -dEPSCrop input.eps output.pdf another useful options to consider: -dPDFSETTINGS=/prepress (or =/print) for a publishing quality optimised output. See GhostScript Documentation for more details. Alternatively, you could use epstopdf input.eps 9. To convert an .pdf-image to .eps, use GhostScript pdf2ps -sDEVICE=epswrite input.pdf output.eps or pdftops -eps input.pdf output.eps The latter is of higher quality but might leave extra white space around the image. 10. PLoS ONE Graphics Preparation: create a .pdf (.eps) with embedded fonts, with the following GhostScript options (note that -dUseCIEColor is an extra option that makes device-independent colours for printing): # create a .pdf from .eps ps2pdf -dEPSCrop -dPDFSETTINGS=/printer -dUseCIEColor \ -dCompatibilityLevel=1.3 -dMaxSubsetPct=100 -dSubsetFonts=true \ -dEmbedAllFonts=true figure.eps figure.pdf # check the fonts pdffonts figure.pdf # convert back to .eps pdftops -eps figure.pdf figure-emb.eps (sources: Osmanoglu's blog, ps2pdf manual, comp.text.tex group); batch-convert .eps to .tiff (lossless with 400 dpi and 8-bit colour depth) for f in *.eps; do convert -density 400 -depth 8 -alpha off -compress LZW$f ${f%.*}.tiff; done. and add a 2px-wide white border around the images mogrify -mattecolor white -frame 2x2 *.tiff (see the figure guidelines). 11. To convert an animated .gif to .avi, use ImageMagick and ffmpeg: convert FILE.gif temp%05d.png ffmpeg -i temp%05d.png -r 10 -qscale 0 FILE.avi rm temp*.png (here -r defines the frame rate in Hz, and -qscale preserves the quality). To convert to other formats, simply change the extention, e.g. to FILE.mov. If you do not have ffmpeg installed, you could do it on Ubuntu with sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next sudo apt-get update sudo apt-get install ffmpeg source: ffmpeg usage tips, documentation and installation. ## Writing & Presenting ### JabRef - BibTeX manager 1. To import a bibliography file from Firefox directly to JabRef, create the following executable bash-script (for Linux users), say JabRef: #!/bin/sh java -jar /PATH_TO_JabRef/JabRef-2.6.jar --import$(readlink -f $1) then browse and select this script (or just JabRef.exe itself in MS Windows) with Open with... in Firefox each time when you encounter a .ris-file or the like. N.B. Do not forget to activate Preferences --> Advanced --> Remote Operation in JabRef. 2. You can use the following sources of bibliography entries for importing directly to JabRef: 1. Publishers (Elsevier, Springer, etc.) by using the options: Export Citation, Export this article as RIS, and so on; 2. Google Scholar (go to Scholar Preferences --> Show links to import citations into BibTeX) 3. World Library Catalogue (go to Cite/Export --> Export a Citation) 4. And many-many others. 3. To change the case of any field to, e.g. First capital only; First Capital In Each Word; all in lowercase or ALL IN UPPERCASE, right-click in the entry editor and choose the desired format from the menu Change case. 4. To push references directly to Kile, using [LyX/Kile] button in JabRef: 1. Create a new directory .lyx in your home folder; 2. Go to JabRef menu Preferences -> External Programmes, click on the [LyX] button, enter /FULL_PATH_TO_HOME/.lyx/lyxpipe.in and save. Now launch Kile, place the cursor at a desired position in your document, select one or several references in JabRef, press [LyX/Kile] button and enjoy imported BibTeX keys. 5. To automatically wrap all capital letters for certain fields in JabRef with { } (to make sure they are preserved in the LaTeX output), go to menu Options -> Preferences -> General -> File -> "Store the following fields with braces around capital letters" and include the desired fields, e.g. title;journal;author. This setting automatically adds braces when saving the .bib file, but you won't see the braces within JabRef (from jabref-users mailing-list). 6. Formatting options for export filters are very flexible. For example, this \begin{author}\format[Authors(LastFirst,NoPunc,InitialsNoSpace),HTMLChars,Replace(A Name,<strong>A Name</strong>)]{\author}\end{author} would print an author list in a compact format with the author "A Name" shown in bold. Source: JabRef mailing list; see more details. 7. To use the LibreOffice plug-in for literature export, install the java API package: sudo apt-get install libreoffice-java-common press Tools --> OpenOffice/LibreOffice Connection, click on [Manual Connect], and set 'path to executable' to /usr/lib/libreoffice/program/soffice.bin and 'path to library dir' to /usr/lib/libreoffice. ### Pdf manipulation with pdftk 1. If you prefer, you can use a GUI interface for pdftk command-line backend: PDF Chain (Linux / MacOS), e.g., in Ubuntu sudo apt-get install pdfchain and original PDFtk Free/Pro (MS Windows) 2. To join together two or more pdf files, use pdftk file1.pdf file2.pdf cat output final.pdf and to glue many sequentially-numbered files, use pdftk *.pdf cat output final.pdf See more PDFtk examples. 3. To rotate the first page of the file.pdf clockwise to 90 degrees, run the following: pdftk file.pdf cat 1east 2-end output fileRotated.pdf 4. To rotate all odd pages to 180 degrees, download pdfrotate script and run ./pdfrotate odd 180 input.pdf output.pdf (it is based on this script) Update: the same effect can be achieved by a standard command: pdftk A=input.pdf shuffle A1-endoddsouth A1-endeven output final.pdf 5. To shuffle the pages from two pdfs, e.g. one with odd sides and one with reverse-order even sides (as produced by an automatic scanner), use pdftk A=odd_pages.pdf B=even_pages.pdf shuffle A1-end Bend-1 output final.pdf 6. To fix a minor structure corruption in given file.pdf, just execute pdftk file.pdf output fileFixed.pdf 7. Lossless PDF compressor smpdf is availabe from the CPDF toolkit: smpdf input.pdf -o output.pdf [Update]: for a newer version of cpdf, use the following syntax instead: cpdf -squeeze input.pdf -o output.pdf Another way is to use a lossy GS-based image-compression script shrinkpdf: ./shrinkpdf input.pdf output-small.pdf #(use 150 dpi instead of default 72) ./shrinkpdf input.pdf output-small.pdf 150 8. PDF Security. To protect a pdf file from opening with "OpenPass" and editing with "MasterPass", use pdftk in.pdf cat output protected.pdf owner_pw MasterPass user_pw OpenPass Alternative command for cpdf (see more CPDF examples): cpdf -encrypt 128bit MasterPass OpenPass in.pdf -o protected.pdf To extract some pages from a protected pdf, use pdftk protected.pdf input_pw MasterPass cat 1 3-end output decrypted.pdf or, better still: pdftk protected.pdf cat 1 3-end output decrypted.pdf do_ask Alternative cpdf command is cpdf -merge protected.pdf owner=MasterPass 1,3-end -o decrypted.pdf Note that when encrypting pdftk by default revokes all permissions (including printing), while cpdf allows printing, copying, etc. unless told otherwise. Also, cpdf preserves original 'creator' metadata and produces slightly smaller files, while pdftk overwrites the metadata with its own. 9. To batch-encrypt pdf files (128 bit), use for f in *.pdf; do pdftk$f cat output ${f%.*}-e.pdf owner_pw MasterPass user_pw OpenPass; done To decrypt and extract the first page, use pdftk protected.pdf cat 1 output first_page.pdf do_ask If no (i.e. empty) user pass is set, one can also use qpdf to decrypt the pdf file: qpdf --decrypt protected.pdf --password="" out.pdf Please make sure you have valid authority to process the pdf files. 10. Tiff-to-Pdf conversion (Caution: doesn't always produce reliable results) 1. join multiple .tiff-files into a single multi-page .tiff tiffcp *.tiff OUTPUT.tiff 2. convert (using jpeg compression) tiff2pdf -j -o OUTPUT.pdf INPUT.tiff Alternatively, use ImageMagick and pdftk: for f in *.tiff; do convert -compress jpeg -quality 75$f ${f%.*}.pdf; done pdftk *.pdf cat output final.pdf 11. Full-text search in multiple PDF files. It requires pdftotext and works only with text-based or OCR PDFs (not image-only scans). Use the following command: find . -name '*.pdf' -exec sh -c 'pdftotext "{}" - | grep --with-filename --label="{}" --color "SEARCH STRING"' \; (See source for more info). 12. For Linux support of proprietary XFA PDF Forms (available in Adobe Reader but not in okular or evince), use Master PDF Editor (free for non-commercial use). ### LaTeX Tips 1. The Python-based computing environment SAGE allows for elaborate plotting capabilities inside the LaTeX-environment: \usepackage{sagetex} ... \sageplot[angle=30, width=5cm]{plot(sin(x), 0, pi), axes=False} 2. To compare changes in two .tex-files use the following: latexdiff old.tex new.tex > diff.tex The perl-script latexdiff doing this is available at CTAN (sometimes it is necessary to use the script named latexdiff-so instead). 3. One way to do word & character-count in a TeX file is to apply the ps2ascii utility to a generated .ps or .pdf as follows: ps2ascii file.pdf | wc The output consists of three figures. The first one is the number of lines, the second is the number of words and the last one is the number of characters (bytes). 4. To deal with a hyperref package warning: Token not allowed in a PDFDocEncoded string..., provide alternative plain text for maths by using \texorpdfstring{$Your Formula$}{Equivalent Text} in \section{}, \subsection{}, etc. (see also other hyperref problems). 5. To prevent Adobe PDF reader from errors like "Cannot extract the embedded font...", add the PDF-specific option -Ppdf to dvips command line (e.g. in Kile/WinEdt settings). Also, avoid using \usepackage[T1]{fontenc} but use {\fontencoding{T1}...} locally instead. 6. To make a book/thesis-style PDF-document with discontinuous page-number navigation, like "page 10 (14 of 200)", ensure the following options are used in \usepackage[...]{hyperref}, NOT in the \hypersetup{...} : \usepackage[plainpages=false,pdfpagelabels]{hyperref} More Pdf/LaTeX-related help (see also the UK TeX FAQ and TUG mailist). 7. To circumvent the LaTeX base warning: Float too big for page... for a Figure/Table that still fits the page, use the minipage environment with plain-TeX \vss to make LaTeX ignorant of extra height: \begin{figure}[p] \begin{minipage}[t][\textheight]{\textwidth} \centering \vspace{-1em} % some tweaking \includegraphics{...} \vspace{-0.5em} \caption{...} \label{fig:...} \vss % hides extra height \end{minipage} \end{figure} and do not forget that \label always go inside or straight after the \caption. See more on comp.text.tex. 8. To prevent floats (Figures and Tables) from "floating away" too far from the desired place, or to have a footnote postponed until the next page, use the \usepackage{afterpage} package, providing a command \afterpage{ \do_something_after_this_page_ends }. For example, to force the queueing floats to be placed as soon as this page ends (without breaking it), put \afterpage{\clearpage} or, if you need to have a footnote to a Figure/Table that wrongly appears at the previous page, use \begin{figure} ... \caption{Blah-blah-blah \protect\footnotemark .} \end{figure} \afterpage{ \footnotetext{Some clarification.} } Note that this method is NOT very robust, particularly if you try to use the command inside the floats. Here is the package documentation and some more info on managing the floats. 9. To avoid an extra space after a full stop, like in Latin 'etc.', 'e.g.', 'ca.', 'cf.', ..., put a backslash just after the full stop: ... e.g.\ we can avoid here an extra space. See more white space info. 10. \textstyle command in the display mode switches fonts and spacing to be a more compact, e.g. \textsyle\sum e^{\textstyle a/b} The other LaTeX maths styles are \displaystyle, \scriptstyle and \scriptscriptstyle. 11. See this wiki-guide for advanced Table Formatting. ### PSTricks - advanced graphics 1. To plot a simple graph of an algebraic function with labels, use \usepackage{pst-math,pst-plot,pstricks} ... \begin{figure}[tp] \centering \psset{unit=1.2cm} % use this to scale the picture \psset{plotpoints=500} % plotting quality (def=50) \begin{pspicture*}(-0.5,-0.5)(5,3) % set up axis: (centrx,centry)(minx,miny)(maxx,maxy) \psaxes[labels=none,ticks=none]{->}(0,0)(0,0)(4.5,2.5) \rput(4.5,-0.25){$t$} % label axes \rput[t](-0.35,2.5){$f(t)$} % plot f(t) = 2*exp(-5*(t-1)), using Reverse Polish Notation \psplot[linecolor=blue,linewidth=1.25pt]{1}{3}% { 1 neg x add 5 mul neg EXP 2 mul } % x is the argument % plot a horizontal line and a label \psline[linecolor=red,linestyle=dashed,linewidth=0.75pt]% (0,1.5)(4,1.5) \rput(-0.25,1.4){\small$\frac{a}{b}$} % plot a vertical line \psset{linecolor=blue,linewidth=1pt,linestyle=dotted} \psline(2,0)(2,2) \rput(2,-0.25){\scriptsize$\tau$} \end{pspicture*} \caption{PSTricks-graph}\label{fig:pstricks-graph} \end{figure} 2. Note that PostScript follow the Reverse Polish Notation, so for some numerical constants a,b,c,d, c/d + b*exp(-a*x) = x a neg mul EXP b mul c d div add See The List of PostScript Maths commands, plus extra commands, like EXP, from the package pst-math. Alternatively, use package pstricks-add, which provides an option \psset{algebraic=true} but the package is sensitive to the LaTeX distribution and prone to instabilities. 3. A curious application to the bar-code generation including 2D Quick-Response code (see also the '\string~' tip to handle tildes in the url correctly). ## Miscellaneous ### Linux General 1. To convert encoding, e.g. from CP1251 (Cyrillic Windows format) to UTF-8 (Linux format), use iconv -f cp1251 -t utf-8 input.txt -o output.txt or, in a loop, to re-encode all files find . -name '*' | while read i; do iconv -f WINDOWS-1251 -t UTF-8 "$i" >tmp; mv tmp "$i"; done To convert the filenames only, use the convmv utility by Bjoern Jacke: convmv -r -f WINDOWS-1251 -t UTF-8 ./* A quick diagnostic summary could also be given by find . | perl -ane '{ if(m/[[:^ascii:]]/) { print } }' > Non-ASCII-filenames.txt 2. To set default file-sorting by ASCII (MC-style), so that -Dir and _File are on top (like in Win & MacOS), (a) in RedHat/CentOS, create a file$HOME/.i18n and put the following line in it: LC_COLLATE=C (b) in Debian/Ubuntu, add the following to $HOME/.profile file: export LC_COLLATE=C then relogin or reboot the PC. (source: CentOS, Arch, Ubuntu). 3. Running MS Windows applications in Cyrillic with wine virtual machine LC_CTYPE=ru_RU.utf8 wine application.exe (see more hints). 4. Printing calendar for year 2010 (week starts from Monday): cal -m 2010 5. To copy text from Midnight Commander editor, hold <Shift> and select desired text by mouse. 6. PC/server performance: check CPU information by using cat /proc/cpuinfo or dmesg | grep CPU (e.g. cat /proc/cpuinfo | grep processor tells the number of processors/cores); check RAM-memory by using cat /proc/meminfo (in kB) or free -m (in MB); check miscellaneous hardware components: cat /etc/sysconfig/hwconf; check the disc-space usage & capacity of mounted file-systems: df -h (add -T for fs type); check the number of users in the system finger ; check the system load by uptime or top (the load average numbers show how many of the processor cores are busy). 7. IP / MAC-address and other networking details: /sbin/ifconfig . 8. To check the error and temperature SMART data for a hard-drive, use smartctl -a /dev/hda in Linux (where the required HDD device name can be found via df -h ), and use a free monitoring tool SpeedFan in MS Windows. 9. To change the modification date of a file/directory into the format YYYYMMDDhhmm (e.g. to 2010-03-02, 0:00), use touch -m -t 201003020000 FILENAME 10. To combine multiple *.csv files in one, use cat *.csv > merged.csv 11. Bulk-renaming the files (reg-ex command requires rename.pl Perl script by Larry Wall): remove/change a prefix in filename rename 'old_prefix-' 'new_prefix-' ./* reformat all the numbers in filenames as 001, 002, ... ./rename.pl 's/([0-9]+)/sprintf("%03d",$1)/e;' ./* replace all spaces in filenames with '-' (remove 'g' after / to replace only the first occurrence) ./rename.pl 's/\s/-/g' ./* find and replace ':' with '-' in all filenames for MS Windows compatibility (a safer version: remove the trailing -n option after a test run) find . -name '*:*' -exec rename 's/:/-/g' {} -v -n \; N.B. It is a good practice to copy the files in a dedicated directory and testing it before running a command. See also a regex reference and an online regex simulator.
12. To backup all dot-files in the $HOME directory, use cd ~ find -regex './\..*' | tar cvfz ./dot_files.tar.gz -T - or find -regex './\..*' -exec tar zvfc dot_files.tar.gz {} + more info. 13. To backup all .bib-files that are not in "reserv" directory to$HOME/BIB_BACKUP/ and then replace "old string" with "new string" in all matched *.bib-files, use mkdir $HOME/BIB_BACKUP find$HOME $$! -regex '.*/.*reserv.*'$$ -iname "*.bib" -exec cp -au {} $HOME/BACKUP \; find ./ $$! -regex '.*/.*reserv.*'$$ -iname "*.bib" -type f \ -exec grep -q OLDSTRING {} \; \ -exec perl -i.bak -pe "s/OLDSTRING/NEWSTRING/g" {} \; \ -printf "Processed file: %p\n" N.B. Instead of Perl, one can use -exec sed -ic "s/oldstring/newstring/g" {} \; as the last operation. Note that {} in the -exec portion is substituted with the found filenames; '\' allows multiline commands, and '\;' terminates each -exec statement. Also note that '{' has to be escaped like '\{' in Perl's 'oldstring'. 14. To copy/move all files of particular type from multiple directories to a single one, use find ROOT_DIR -iname "FILETYPE" -exec cp {} TARGET_DIR \; , e.g. the following moves recursively all mp3-files from current directory to './global_storage/' find ./ -iname "*.mp3" -exec mv {} ./global_storage/ \; 15. To show all files in HOME directory (except the hidden '.files') that have been modified in the last 24 hours, use find$HOME $$! -regex '.*/\..*'$$ -type f -mtime 0 -mtime -n means during the last n days
16. To create a zip archive from a batch of files (or a directory, recursively) use zip -rTog filename.zip /PATH/* Note, that you can add -m to delete these files after archivation.
17. To create an encrypted 7z archive from a directory (preserving all attributes), use tar cf - DIR | 7z a -si -p DIR.tar.7z or, without preserving the ownership, and encrypring the file listing header: 7z a -mhe=on -p DIR.7z DIR Alternatively, you could create a less well-compressed zip-file, using 7z a -p DIR.zip DIR (Note that an appropriate way to exract files is "7z x FILE").
18. To install an application, e.g. Midnight Commander, from source (.tar, .tar.gz) to your home directory (root-level-access to your PC is not needed), use the following:
2. unpack: tar zvfx mc-4.6.1.tar.gz
3. cd mc-4.6.1
4. configure: ./configure --silent --prefix=$HOME/.mc (here --prefix says where to install, subject to your rights to do so, and --silent suppresses the auxiliary output) 5. compile: make 6. install: make install That is it. The binary file is usually found in bin/ directory (in this case you can run it as$HOME/.mc/bin/mc). However obvious it may sound, it is better to have all bits and bobs of a standard procedure ready at hand.
19. To synchronise the directory modification time to match the latest date of its content, run the command ./synch_dir PATH_TO_DIR, where PATH_TO_DIR is the root path to the directory tree to be synchronised and synch_dir is the following shell script #!/bin/sh
# run for every child directory of $1 find$1 -type d | while read path; do
# get the latest modified file in the tree $path fname=$(find "$path" -type f -printf "%T+\t%p\n" | sort -n | tail -1 | cut -f2); # set the modif. date of$path to match the attribute of $fname touch -m -c -r "$fname" "$path"; done (based on tip 1, tip 2 and tip 3) 20. To change access rights to all files and directories recursively, e.g. to be readable and browsable to others, use chmod -R u=rwX,g=rX,o=rX ./* 21. To prevent accidental deletion of content created by others in a shared directory, use a 'sticky bit': chmod +t ./SHARED 22. To recover an accidentally deleted file, use PhotoRec and for a more in-depth recovery of disk partitions, boot sector or files, use TestDisk: sudo ./photorec_static sudo ./testdisk_static Note that PhotoRec seems more straightforward and reliable on FAT32 USB stick-like media. See more details on using the PhotoRec and TestDisk. 23. To compare two text-files, use diff --side-by-side File1 File2 (a more advanced alternative is to use IDE like Eclipse). 24. Midnight commander font and colour settings: i. KDE konsole seems to provide the most clean look and colour scheme for mc (settings -> edit profile -> appearance -> black on white). Alternatively, use gnome-terminal with edit -> profile preferences -> colours -> pallete -> Linux console. ii. download and use Terminus font. iii. see more mc colour theme options. 25. Ubuntu package managment: install a package (you could use [TAB] key for suggested packages) sudo apt-get update sudo apt-get install PACKAGE_NAME # for installing from a repository sudo dpkg -i PACKAGE_FILE.deb # for a downloaded file resolve missing dependencies sudo apt-get install -f search for a package by its partial name or a name of the file it contains apt-cache search PACKAGE_NAME apt-file search .so-FILE_NAME completely remove a package with its configuration files sudo apt-get remove --purge PACKAGE_NAME general clean-up sudo apt-get autoremove sudo apt-get autoclean delete old Linux kernels sudo apt-get remove --purge$(dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/$$.*$$-$$[^0-9]\+$$/\1/")"'/d;s/^[^ ]* [^ ]* $$[^ ]*$$.*/\1/;/[0-9]/!d') 26. Symbolic link maintenance tool to convert absolute to relative symlinks: # test symlinks -rst ~/disk/* # run symlinks -rsc ~/disk/* 27. To automatically restore applications running in Gnome 3 after logging out, run gnome-session-properties (e.g. with Alt + F2) and select Options --> Automatically remember running applications... Note however that this could lead to some instability. 28. If there is some mix-up with default application associations, have a look in ~/.local/share/applications/mimeapps.list. 29. To add a shell script (or custom application) to the list of "Open with...", create a new MyApp.desktop in ~/.local/share/applications/ with the following content: [Desktop Entry] Version=1.0 Type=Application Terminal=false Exec=/PATH_TO_APP/MyApp.sh %f Name=MyApp Icon=/PATH_TO_APP/MyApp-icon.png To permanently associate file-type with an App, right-click on it and then select properties -> open with -> set as default. (see also .desktop format specification) 30. To set up application shortcuts in Gnome, run gconf-editor, then go to apps -> metacity -> global_keybindings and afterwards to keybinding_commands. Note, that in Gnome 2 MATE, you need to run dconf-editor and find org -> mate -> marco -> global_keybindings respectively. General dconf manipulations: dconf dump / | grep keyword :: search dconf for an entry dconf reset -f /org/matel/panel/objects/object-n/ :: delete phantom object n 31. To sort folders before files in Gnome 3.12+, run the following in the terminal: gsettings set org.gnome.nautilus.preferences sort-directories-first true gsettings set org.gtk.Settings.FileChooser sort-directories-first true (similar effect can be done via GUI dconf editor; source). 32. ==== Remote Access Tweaks and Settings ==== To open a remote Linux shell in a panel of Midnight Commander via ssh, use Shell link, where you enter server hostname (e.g. 'granby'), or the full path to the remote machine. 33. To run X11 applications from MS Windows (a la ssh -X): i. Install Win X-server, e.g. XMing ii. Install PuTTy iii. Setup PuTTy -> Connection -> SSH -> X11-> Enable iv. Use$ xfe as simple GUI, or $mc as text-style, file-manager (install sudo apt install xfe on the target Linux server, if necessary) source 34. To protect a remotely running SSH process from termination, use nohup:$ ssh username@remote-server
$nohup my_command > my_command.log 2>&1 &$ exit
which outputs 'my_command' stdin and stderr to 'my_command.log'. An alternative is to use disown: $my_command > my_command.log 2>&1 & # script or GUI$ disown -a && exit
(see this reference more details). However, a still better and more flexible way is to use a virtual terminal such as screen: see an example of using screen to detach/re-attach a remotely running Malab script.

### Firefox

1. To re-enable a stand-alone Preferences Dialogue in Firefox 38+, type in about:config in the address field and set browser.preferences.inContent = false
browser.preferences.instantApply = false
2. To open pdf-documents with a stand-alone application instead of loading to the Firefox tab about:config -> New -> String -> plugin.disable_full_page_plugin_for_types = application/pdf
3. To switch off the irritating "smart" search in the address bar of Firefox 3, use about:config -> browser.urlbar.matchOnlyTyped = False
4. To turn off the compatibility check of add-ons, you can create a new property set about:config -> New -> Boolean -> extensions.checkCompatibility.3.6 = False (where 3.6 is the first two numbers of Firefox's version). A better way, however, is to install a Compatibility Reporter that does this automatically for you.
5. To disable a recently (ver. 41+) introduced 'add-on verification' feature, set about:config -> xpinstall.signatures.required = False
6. A useful set of add-ons for Firefox: Adblock and Flashblock (anti ad-banners: image- and flash-based); Abduction (save page as image); Tab Mix Plus and FaviconizeTab (tab-management) to activate multi-row tabs go to Tools->TabMixPlus Options->Display and select "When tabs don't fit width" to be Multi-row; PinboardButton, del.icio.us, Google Notebook (web-services); FireBug, LinkChecker, Tamper Data - HTTP headers, ShowIP (web-development tools); All-In-One Sidebar, Copy All Urls, CuteMenus2, Mr Tech Toolkit (misc utilities)
7. If something is wrong with Firefox, you can create and launch a temporary blank new profile by typing (from the command line) firefox -ProfileManager (or firefox -P) and then hitting the [Create Profile...] button
8. Use firefox -no-remote to run a new firefox instance rather than opening another window to existing process.

### Thunderbird

1. Some indispensable add-ons for Thunderbird: Message Archive Options (included in version > 3.1 but still useful), Archive This, Keyconfig (edit key-bindings), CompactHeader, Outgoing Message Format (to set "plain text and HTML" instead of auto-detect), Signature Switch (to add a signature on/off button), Send Later (for timed sending), Remove Duplicate Messages, Show All Body Parts (switch between HTML and explicit attachment mode), DOM Inspector, Copy Folder (synchronise IMAP/POP and local folders), Manually Sort Folders (to change the order of accounts in the left panel), Mail Merge (customised newsletter sender; see more details), SmartTemplate4 + Stationery (flexible email templates).
2. To auto-hide tab toolbar in Thunderbird 3.*- , thus not wasting some screen space, go to Edit -> Preferences -> General -> Config Editor and change the following property mail.tabs.autoHide = true source
3. To get rid of the star next to the email address in Thunderbird 3.-, add .emailStar {display: none} to the /chrome/userChrome.rss which has to be put in the user profile (e.g. ~/.thunderbird/***.default/ in Linux).
4. More tweaks and hacks for toolbar/menu items can be done with DOM Inspector add-on.
1. Tools -> DOM inspector: File -> Inspect Chrome Document -> 'Inbox... Thunderbird'
2. Either locate an element with 'find a node' button (top-left) or browse the layout structure (e.g. #document/window/menuPopUp(mailContext) to configure right-click menu)
3. to hide the menu-item, add hidden="true" to the right-hand-side 'Attributes' panel
4. you can move a menu-item/button around by copy-cutting its node across the document tree.
5. To set a maximal number of recent folders in MoveTo->Recent menu, install the following add-on chrome.xpi (first setting extensions.alwaysUnpack to true in the Settings->Advanced->Config Editor) and then follow the instructions.
6. To clear the list of Recent folders in "Move to" and "Copy to", remove the file panacea.dat from the main Thunderbird profile directory (source).