Use of \tikzexternalize - compilation

I want to improve compiling speed of my latex thesis. I have read that using \tikzexternalize, only when there is a change in the tikz codes they are compiled.
The problem is that I can no use \tikzexternalize properly. This is my preamble:
\usepackage{circuitikz}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows, shadows, calc,positioning}
\tikzexternalize
And this is not working at all. I received next error:
Undefined control sequence. \tikzexternalize
If it is useful, I am using MikTex with TextStudio and TextLive with Textstudio also under Ubuntu. It doesn't work in any of these system.
Hope you can help me.

Didn't you forget to add
\usetikzlibrary{external}
? I just found it here : tex.stackexchange

I have done it.
Now I don't have any error messages, but no figures are saved into TikzFigures folder. This is my preamble:
\usepackage{circuitikz}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows, shadows, calc,positioning,external}
\tikzexternalize[mode=graphics if exists,
figure list=true,
prefix=TikzFigures/]
This is how I add my tikz figures. First example is by using circuitikz.
\begin{figure}[h]
\centering
\tikzsetnextfilename{nonInverter}
\begin{circuitikz}[scale=1]\draw
(4,.5) node [op amp](opamp) {} % Amplificador Operacional
(0,0) node [ ground]{} to (0,1)
to [R=$Sensor A201$,-*] (opamp.-)
(opamp.out)|- (5,2.5) to [R=$R_{F}$](2.8,2.5) to (opamp.-)
(opamp.+) to [short,-o] (2.8,-1) node [right] {$V_{+}$}
(opamp.out) to [short, *-o] (6,.5) node[right] {$V_{ADC}$}
;\end{circuitikz}
\end{figure}
Next one, is by using the common tikz package:
\begin{figure}[h]
\begin{center}
\tikzsetnextfilename{masterFlowChart}
\begin{tikzpicture}[node distance = 2cm, auto]
% Colocamos los nodos
\node [cloud] (init) {Initialize System};
\node [block, below of=init] (connection) {Connection to the master};
\node [block, below of=connection] (wait) {Wait for master events};
\node [block, below of=wait] (measure) {Measure and send to master};
% invisible node helpful later
\node[right of=wait,scale=0.05](inv){};
% Colocamos las relaciones entre nodos
\path [line] (init) -- (connection);
\path [line] (connection) edge[loop left] node {No Network}();
\path [line] (connection) -- (wait);
\path[-,draw] (wait) -| node{} (inv.north);
\path[line]{} (inv.north) |- node[above]{Reconnect} (connection);
\path [line] (wait) edge[loop left] node {Sleep}();
\path [line] (wait) -- (measure);
\path [line] (measure) -- (wait);
\end{tikzpicture}
\end{center}
\label{fig:slave_flowchart}
\end{figure}
And last one is by using sequencediagram package, that if I am not wrong is also based on tikz.
\begin{figure}[h]
\begin{center}
\tikzsetnextfilename{sequenceDiagramaECnsole10}
\begin{sequencediagram}
\newthread{pc}{PC}
\newinst[2]{master}{Master}
\newinst[2]{slave}{Slave}{}
\node[below right of=master,font=\scriptsize](master_state){Idle};
\node[below right of=slave,font=\scriptsize](slave_state){Sleep};
\begin{sdblock}{Run Loop}{}
\begin{call}{pc}{Start Measurement}{master}{Master Data}
\begin{call}{master}{Data Request}{slave}{Slave Data}
\end{call}
\end{call}
\end{sdblock}
\node[below of=master_state,font=\scriptsize, node distance= 4cm]{Idle};
\node[below of=slave_state,font=\scriptsize, node distance= 4cm]{Sleep};
\end{sequencediagram}
\end{center}
\label{fig:ecnsole_sequence_diagram}
\end{figure}

Related

Is it possible to align LaTeX lines of code to the left instead of the right?

I've just started using LaTeX that is included in MacOS' Pages. It's been really helpful for writing physics formulas and such, but I tried to write a solution with multiple lines for a lab report, and everything is aligning to the right. I can't find anything online to move it leftwards; a lot of commands don't work with it.
Here's what I have:
cosΘ = {B_{horizontal}\over B_{total}} \\
cosΘ = {98\over 132} \\
cosΘ = 0.7424242424 \\
Θ = cos^{-1}0.7424242424 \\
Θ = 42.1\,\,degrees
\end{align}
But it's showing up
like this.
Is there a way to get it aligned like the picture, but to the left instead of the right? I've tried for at least an hour to find a solution that works to display what I want.
Thank you in advance! Please know I'm new to this and have only been using this for about two months. I'm sorry if I'm missing anything obvious!
Use & to set the points where the equations are vertically aligned. And instead of cos use the command \cos. I also use \text.
\begin{align}
\cos \theta &= \frac{B_{\text{horizontal}}}{B_{\text{total}}} \\
\cos \theta &= \frac{98}{132} \\
\cos \theta &= 0.7424242424 \\
\theta &= \cos^{-1}0.7424242424 \\
\theta &= 42.1 \text{ degrees}.
\end{align}
I misread your question and assume that you would prefer to align the =. To align the lines to the left, you can use the & at the beginning of the lines.
\begin{align}
&\cos \theta = \frac{B_{\text{horizontal}}}{B_{\text{total}}} \\
&\cos \theta = \frac{98}{132} \\
&\cos \theta = 0.7424242424 \\
&\theta = \cos^{-1}0.7424242424 \\
&\theta = 42.1 \text{ degrees}.
\end{align}

How to allocate the label center of case using sphinx?

I'm writing equations using sphinx. My code looks like this:
.. math::
:label: eq1
\begin{eqnarray}
\begin{cases}
a_{11}x_{1} + \dots + a_{1m}x_{m} = b_1\\
a_{21}x_{1} + \dots + a_{2m}x_{m} = b_2
\end{cases}
\end{eqnarray}
This displays the label (1) on the right side of the first line, but I would to allocate it the center of this {cases}, namely, between the first and the second line.
How do I do this?
This question seems to pertain principally about MathJax usage.
By some experimenting I discovered it understands \smash and that using it provides the hoped for label placement. But perhaps there is some MathJax setting which would avoid using this \smash. Besides \smash works fine only for two or three "cases".
I have moved the \smash approach to second part, where also its drawbacks are explained. Indeed, I have since found a half-satisfying CSS approach.
At any rate, don't use eqnarray there.
CSS based approach
Create a file _static/custom.css in your source repertory with these contents.
.MathJax_Display {
transform: translate(0%,-50%) translate(0%,8px);
}
div.math {
transform: translate(0%,50%);
}
Add this at end of conf.py:
if html_theme != 'alabaster':
def setup(app):
app.add_stylesheet('custom.css')
For example I obtain this with 'classic' theme:
This works also with 'agogo' and 'alabaster' themes, but some fine tuning might be needed for equations with only one line to be positioned like the label.
In the case RTD theme it does not work at all.
Support may be browser dependent.
\smash approach
Text before.
.. math::
:label: eq1
\smash{\begin{cases}
a_{11}x_{1} + \dots + a_{1m}x_{m} &= b_1\\
a_{21}x_{1} + \dots + a_{2m}x_{m} &= b_2
\end{cases}}
Text after.
I get from make html (with classic theme)
conf.py contains
extensions = ['sphinx.ext.mathjax',
]
html_theme = 'classic'
By the way your eqnarray is not good mark-up. IF you use it you should use :nowrap: option. See Sphinx doc.
Note Of course usage of cases here is dubious because your example is one of equations. I added &'s but the spacing is one appropriate for cases, which is surely not the one expected. So remove them.
Here is mark-up which does not abuse cases environment:
Text before.
.. math::
:label: eq1
\smash{\left\{\begin{aligned}
a_{11}x_{1} + \dots + a_{1m}x_{m} &= b_1\\
a_{21}x_{1} + \dots + a_{2m}x_{m} &= b_2
\end{aligned}\right.}
Text after.
CAVEAT The smash trick only works with two (perhaps three) equations, even in MathJax.

What's the best approach to generate image based on user submitted text on server side like Rails

Guys,
I see ImageMagick is capable to generate image using Pango formatted text, which looks like quite a good approach.
Just want to know if there's anything else out there, what's the most recommended way of doing this.
imagemagick is probably the easiest, but ghostscript can also be used to render images with text.
Here's a little postscript program that displays some text.
%!
5 5 moveto
/Palatino-Roman 20 selectfont
(Some Text) show
showpage
Using ps2eps will calculate the Bounding-Box and add this information as a comment conforming to the Document Structuring Conventions.
%!PS-Adobe-2.0 EPSF-2.0
%%BoundingBox: 5 5 97 20
%%HiResBoundingBox: 5.500000 5.000000 97.000000 19.500000
%%EndComments
% EPSF created by ps2eps 1.64
%%BeginProlog
save
countdictstack
mark
newpath
/showpage {} def
/setpagedevice {pop} def
%%EndProlog
%%Page 1 1
5 5 moveto
/Palatino-Roman 20 selectfont
(Some Text) show
showpage
%%Trailer
cleartomark
countdictstack
exch sub { end } repeat
restore
%%EOF
Then imagemagick's convert utility can render this as an image.
The ps2eps is necessary so the final image is cropped to the interesting part, rather than at the bottom of a page-sized image.
Here's a typescript of the whole sequence. 0> is the command prompt.
0> cat > t.ps
%!
5 5 moveto
/Palatino-Roman 20 selectfont
(Some Text) show
showpage
0> ps2eps t.ps
Input files: t.ps
Processing: t.ps
Calculating Bounding Box...ready. %%BoundingBox: 5 5 97 20
Creating output file t.eps...** Warning **: Weird heading line -- %! -- ready.
0> convert t.eps t.png

Haskell Graphics Library that works in GHCi on MacOS X

Does there exist a Haskell graphics library or binding to an external library that fulfills the following requirements:
Can be used from ghci, i.e. I don't have to link and restart the program.
Works on MacOS X. (Tricky in conjunction with 1!)
Can do simple vector graphics (lines, polygons, simple fills and strokes).
Can put bitmap images on screen. Example: blit a 17x12 .bmp image.
?
Please include a minimal source code example or a reference to it (just a window on screen, maybe with a green line drawn inside it) so that I can check the points 1. and 2. in particular. Also, if one of these feature requests is more elaborate (for example OpenGL + 4), please include a good reference.
PS: Concerning 1 and 2, I know about the enableGUI trick and I am willing to use it. However, most libraries have the problem that you can't run the main function multiple times and hence don't qualify.
Edit: To avoid wasting your time, here a list of packages that I've tried:
wx - ghci chokes on libstdc++
sdl - redefines main to be a macro. Compile-time only.
GLFW (OpenGL) - Can't run main twice, something about "failing because it can't install mouse event handler".
EDIT: Actually, I'm no longer sure. Several versions later, it seems that GLFW no longer works in GHCi on OS X.
It turns out that GLFW+OpenGL fulfills all four requirements!
You need to invoke ghci with ghci -framework Carbon.
You need the EnableGUI.hs file, which you can get here. Note that you can't load it right into GHCi, you have to comiple it, first.
OpenGL has a 2D projection mode where you can draw lines and polygons.
Bitmaps can be loaded as textures and put on polygons.
Here is a small example that puts a bitmap onto the screen. There are some restrictions on the bitmap: its dimensions must be a power of two (here 256) and it must be a .tga file (here "Bitmap.tga"). But since transparency is supported, this is not much of a problem.
You should be able to call main multiple times without problem. The key point is that you should not call GLFW.terminate.
import Graphics.Rendering.OpenGL as GL
import qualified Graphics.UI.GLFW as GLFW
import Graphics.Rendering.OpenGL (($=))
import Control.Monad
import EnableGUI
main = do
enableGUI
GLFW.initialize
-- open window
GLFW.openWindow (GL.Size 400 400) [GLFW.DisplayAlphaBits 8] GLFW.Window
GLFW.windowTitle $= "Bitmap Test"
-- enable alpha channel
GL.blend $= GL.Enabled
GL.blendFunc $= (GL.SrcAlpha, GL.OneMinusSrcAlpha)
-- set the color to clear background
GL.clearColor $= GL.Color4 0.8 0.8 0.8 0
-- set 2D orthogonal view inside windowSizeCallback because
-- any change to the Window size should result in different
-- OpenGL Viewport.
GLFW.windowSizeCallback $= \ size#(GL.Size w h) ->
do
GL.viewport $= (GL.Position 0 0, size)
GL.matrixMode $= GL.Projection
GL.loadIdentity
GL.ortho2D 0 (realToFrac w) (realToFrac h) 0
render <- initialize
loop render
GLFW.closeWindow
loop render = do
-- draw the entire screen
render
-- swap buffer
GLFW.swapBuffers
-- check whether ESC is pressed for termination
p <- GLFW.getKey GLFW.ESC
unless (p == GLFW.Press) $ do
-- sleep for 1ms to yield CPU to other applications
GLFW.sleep 0.001
-- only continue when the window is not closed
windowOpenStatus <- GLFW.getParam GLFW.Opened
unless (windowOpenStatus == False) $
loop render
-- rendering
initialize = do
-- load texture from file
GL.texture GL.Texture2D $= Enabled
[textureName] <- GL.genObjectNames 1
GL.textureBinding GL.Texture2D $= Just textureName
GL.textureFilter GL.Texture2D $= ((GL.Nearest, Nothing), GL.Nearest)
GLFW.loadTexture2D "Bitmap.tga" []
return $ do
GL.clear [GL.ColorBuffer]
GL.renderPrimitive GL.Quads $ do
GL.texCoord $ texCoord2 0 0
GL.vertex $ vertex3 (0) 256 0
GL.texCoord $ texCoord2 0 1
GL.vertex $ vertex3 (0) (0) 0
GL.texCoord $ texCoord2 1 1
GL.vertex $ vertex3 256 (0) 0
GL.texCoord $ texCoord2 1 0
GL.vertex $ vertex3 256 256 0
-- type signatures to avoid ambiguity
vertex3 :: GLfloat -> GLfloat -> GLfloat -> GL.Vertex3 GLfloat
vertex3 = GL.Vertex3
texCoord2 :: GLfloat -> GLfloat -> GL.TexCoord2 GLfloat
texCoord2 = GL.TexCoord2
color3 :: GLfloat -> GLfloat -> GLfloat -> GL.Color3 GLfloat
color3 = GL.Color3
Here an example bitmap (which you need to convert to .tga).
The Gtk2Hs library fulfills all the requirements if you use the X11 version of the gtk2 framework.
Concerning the requirements:
Using X11 avoids many problems.
Install gtk2 via MacPorts and use the +x11 option (default). (That said, I've had numerous problems installing gtk2 in the past, but this time it seemed to work.)
I would be surprised if GTK+ can't do that.
Ditto.
Here a minimal example
import Graphics.UI.Gtk
hello :: (ButtonClass o) => o -> IO ()
hello b = set b [buttonLabel := "Hello World"]
main :: IO ()
main = do
initGUI
window <- windowNew
button <- buttonNew
set window [windowDefaultWidth := 200, windowDefaultHeight := 200,
containerChild := button, containerBorderWidth := 10]
onClicked button (hello button)
onDestroy window mainQuit
widgetShowAll window
mainGUI
As of early 2014, I wasn't able to use #heinrich-apfelmus answer in Mac OS X. This GLFW-b example (link) however worked.
So, ensure you have:
$ cabal install glfw-b
and, if you tried Apfelmus' answer, you may need to
$ ghc-pkg list
$ ghc-pkg unregister GLFW-x.x.x.x
as both provide Graphics.UI.GLFW, and you will get an "Ambiguous module name 'Graphics.UI.GLFW'" from ghc. Then I just tried the sample program above and it worked (Mac OS X, 10.9, Mavericks)
Have you seen the GLFW as referenced http://plucky.cs.yale.edu/soe/software1.htm
More information on Haskell+GUI+OpenGL is available in this discussion:
http://www.haskell.org/pipermail/haskell-cafe/2011-May/091991.html

How can I determine if a file is an image on the JVM?

I'd like to get the contents of a directory that contains arbitrary files (a typical "Downloads" directory) and determine programmatically if a given file is an Image of any type.
I'm working in Clojure but anything available on the JVM is fair game.
Thanks in advance!
You can use the Tika library that is able to detect many types of files, and also extract metadata from many of them. I have very simple Clojure wrapper for it
Ended up being able to solve this by combining the comment on your question along with my earlier answer from here. Minor change to the code let it work with images that are not images.
I didn't change it to recurse to sub directories. Would be easy enough to do.
(defn files-in-dir [dir]
(filter #(not (.isDirectory %))
(.listFiles (java.io.File. dir))))
(defn figure-out-height-width
[files]
(remove nil?
(map (fn [file]
(with-open [r (java.io.FileInputStream. file)]
(if-let [img (javax.imageio.ImageIO/read r)]
[file (.getWidth img) (.getHeight img)])))
files)))
user> (pprint (files-in-dir "/home/jmccrary/Downloads/"))
(#<File /home/jmccrary/Downloads/Girl_Talk_-_All_Day_(IA123)_mp3s.zip>
#<File /home/jmccrary/Downloads/CSS3-for-Web-Designers.zip>
#<File /home/jmccrary/Downloads/manual.pdf>
#<File /home/jmccrary/Downloads/test.jpeg>
#<File /home/jmccrary/Downloads/nautilus-dropbox_0.6.7_amd64.deb>
#<File /home/jmccrary/Downloads/rubygems-1.3.7.tgz>
#<File /home/jmccrary/Downloads/HTML5-FOR-WEB-DESIGNERS.zip>
#<File /home/jmccrary/Downloads/bcompare-3.1.11.12238.tar.gz>
#<File /home/jmccrary/Downloads/shared_ptr_example.cpp>)
nil
user> (figure-out-height-width (files-in-dir "/home/jmccrary/Downloads"))
([#<File /home/jmccrary/Downloads/test.jpeg> 32 32])
After thinking about it for a bit it feels dirty to combine the check for a file being an image with the pulling out the width and height. Alternatively you could define a function which does this filtering separately and gives you a seq of images.
(defn filter-images
[files]
(reduce (fn [res file]
(if-let [img (javax.imageio.ImageIO/read file)]
(conj res img)
res))
[]
files))
user> (filter-images (files-in-dir "/home/jmccrary/Downloads"))
[#<BufferedImage BufferedImage#24753433: type = 5 ColorModel: #pixelBits = 24 numComponents = 3 color space = java.awt.color.ICC_Color\
Space#43036651 transparency = 1 has alpha = false isAlphaPre = false ByteInterleavedRaster: width = 32 height = 32 #numDataElements 3 \
dataOff[0] = 2>
]
Obviously, the easiest thing to do is look at the filename extension. Of course, it's not necessarily reliable, but it may suffice in some circumstances.
Short of reading the whole image, you could read the first few bytes of the file to identify it by its "magic number". For example, JPEG files always start with the two bytes 0xFFD8 and end with 0xFFD9; PDFs always begin with the string "%PDF".
This saves you the overhead of creating an image in memory, and might speed up your I/O as well (since you only need a few bytes of the file).
If you don't want to research all these magic numbers yourself, you could try a library such as jMimeMagic. I've never used it, so I can't vouch for its quality or completeness, but it is LGPL. I'm sure you can find other alternatives as well.

Resources