This shows you the differences between two versions of the page.

Link to this comparison view

latex:tikz:scalebar [2013/07/04 08:45]
latex:tikz:scalebar [2020/06/10 21:43] (current)
Line 1: Line 1:
 +====== fancy way to generate scale bars with Tikz ======
 +The way to display scale bars shown [[:​latex#​scale_bars|here]] is nice, but can be a bit cumbersome if the images are not that easy, e.g. the image shows a skewed sample in 3D. Everything shown below could also be done completely by hand, but it's quite a boring task to calculate multiple [[http://​www.dict.cc/?​s=dreisatz|rules of three]] for every image you want to add a scale bar. I've written a small MATLAB-script to facilitate the calculation and addition to LaTeX.
 +You'll need:  {{  :​latex:​tikz:​scalebarimage.jpg?​128|}}
 +  * an image you know the scale of and want to draw the scale bar on, e.g. the image shown on the right((it'​s an MRI of my head...))
 +  * MATLAB with this m-file: {{:​latex:​tikz:​scalebar.m|}} (or any other graphics-capable calculation program like [[http://​www.gnu.org/​software/​octave/​|Octave]])
 +  * this minimal tex-file: {{:​latex:​tikz:​scalebar.tex|}} (I suppose you have a running installation of LaTeX to work with this example...)
 +Both files are explained in a bit more detail at the end of this page, for now I'll just walk you through:
 +===== WorkFlow =====
 +  - Start MATLAB, load scalebar.m and start it.
 +  - The script prompts you to load an image, choose "​scalebarimage.jpg"​ you've downloaded from above.
 +  - The script prompts you to define the length of the scale bar you're gonna draw in the next step, choose the default "21 cm" for now
 +  - The script displays the image of my head and asks you to define a start-point of the scale-bar, choose the tip of the nose
 +  - After that, choose the back of my head for the end of the scale-bar
 +  - Note the "​Positions"​ shown in the help-dialog,​ these are to make sure you've clicked correctly and that the positions are correct.
 +  - copy the output in the command-line-section of MATLAB
 +  - paste this output into your .tex-file
 +  - if the .tex file and the image are in the same folder, just proceed, if not, change the path to the image!
 +  - compile the file
 +  - Figure 1 of the document should show
 +    * the exact same scale-bar you've drawn in MATLAB overlayed over the image.
 +    * a second scale bar which shows a reasonable length (5cm in our case){{ ​ :​latex:​tikz:​scalebarfigure.png?​256 ​ |}}
 +  - change \def\x{100} and \def\y{100} on lines 24 and 25 of the copied text to position the scale bar where you'd like to have it and uncomment line 29 to turn off the original scale bar.
 +  - enjoy typographically correct scale bars in the resulting {{:​latex:​tikz:​scalebar.pdf|pdf}} (47 kB)
 +  ​
 +===== LaTeX-File =====
 +The preamble is nothing too fancy
 +<code latex>
 +\usepackage{tikz} ​                % used for actually drawing the scale bar
 +\usepackage[ansinew]{inputenc} ​   % is used so I can write my name without too much hassle ​
 +\usepackage{siunitx} ​             % excellent package to correctly typeset units
 +\usepackage{hyperref} ​            % used for URLs
 +The block below is used to start the document and to set up the image
 +<code latex>
 +\author{David Haberthür}
 +\title{David Haberthür'​s Head - Scalebar}
 + \centering
 +Inside here you'll copy the result from the MATLAB-skript,​ the rest ist just to finish off the document.
 +<code latex>
 +\caption{Three dimensional visualization of David Haberthürs Head, released under a \href{http://​creativecommons.org/​licenses/​by/​2.0/​deed.en_GB}{creative commons Attribution 2.0 Generic license} by the owner of the head.}
 + \label{fig:​head}
 +===== MATLAB-File =====
 +The block below asks the user to select an image-file that is then subsequently used for calculations
 +<code matlab>
 +[ filename, pathname] = ...
 +     ​uigetfile({'​*.jpg;​*.tif;​*.png;​*.gif','​All Image Files';​...
 +          '​*.*','​All Files' },'​Choose Input Image'​);​
 +image=imread([pathname filesep filename]);
 +The scale bar is defined by asking the user to click on the image for a start- and end-point.
 +<code matlab>
 +h = helpdlg('​choose start-point of scale bar','​ScaleBar'​);​
 +[ x1,y1 ] = ginput(1);
 +the rest is just simple, but boring calculations and semi-nice output, which takes care of setting up the necessary lengths for TikZ.
 +===== References =====
 +TikZ: http://​kogs-www.informatik.uni-hamburg.de/​~meine/​tikz/​\\
 +MATLAB: http://​blinkdagger.com/​matlab/​matlab-querying-the-user-for-input