Python (3.5) tool to convert .asc files into circuiTikz graphics
This script takes a LTSpice (IV, XVII) circuit file (*.asc) and tries to convert it to a LaTeX document containing a pgf/TikZ picture of the schematic, using the CircuiTikZ package to draw the symbols. The document can then be compiled, e.g. into PDF format.
Take a look at this example PDF file produced from this sample document (direct comparison).
Currently supported features:
LTSpice (IV, XVII) circuit file (*.asc)
Python 3.4+ or Windows system when using precompiled executable. (3.4 is currently the latest python version that works with py2exe, in case you want to build your own executable).
Copies of the symbol files (*.asy) of all used circuit elements in the sym32a subfolder of the script at the same relative path as they are in your LTSpice library
sym32a\circuiTikz
directory to your LTSpice library sym directory (C:\Users\[username]\Documents\LTspiceXVII\lib\sym
on Windows with version XVII, or [LTSpiceIVInstallDir]\lib\sym
for version IV). You don't need to copy the *.asy2tex files, but LTSpice is smart enough to ignore them if you do so.circuiTikz
folder in your LTspice lib directory should be at lib\circuiTikz
. Do not copy the sym32a
folder itself to lib
, only its circuiTikz
subdirectory!Conversion files *.asy2tex with the same name as the symbol. Symbol files and conversion files for many circuit elements are packaged with the script.
LaTeX with the standalone, circuitikz, tikz, pgfplots, packages installed. The default preamble also requires amsmath, amssymb,bm,color,pgfkeys,siunitx,ifthen,ulem, but you might be able to get along without them by removing the \usepackage lines.
To convert a file just call lt2ti.py with the full path to the *.asc file you want to convert as argument. E.g. if you want to convert the file D:\testdrive\catalog.asc, with the script in D:\scriptdir\LT2circuiTikz\ run
python "D:\scriptdir\LT2circuiTikz\lt2ti.py" "D:\testdrive\catalog.asc">run.log
or, when using the executable
"D:\scriptdir\LT2circuiTikz\lt2ti.exe" "D:\testdrive\catalog.asc">run.log
You will then find your converted file at D:\testdrive\catalog.asc.tex
Compile this file with pdflatex
pdflatex "D:\testdrive\catalog.asc.tex"
to finally get a PDF document D:\testdrive\catalog.asc.pdf with your shematic.
Please note that you might want to change the extension to a simple .pdf in order to not confuse LaTeX when then using the PDF in another LaTeX document.
Use the supplied test_lt2ti.bat
file to test your python setup with lt2ti.py on an example file.
Use examples\compile_catalog.bat
to compile a pdf from the catalog example file to test your latex setup.
The expected outputs are present in the package (and get overwritten, so create copies if you want to compare the results. Alternatively, use git to diff for deviations).
If you want to automate this script even further, you can use it as a python module:
import lt2ti;
fn = r'examples\catalog.asc';
l2tobj = lt2ti.lt2circuiTikz();
l2tobj.readASCFile(fn);
l2tobj.writeCircuiTikz(fn+r'.tex');
If you want to quickly create large, professional looking, publication ready schematics, this script will vastly reduce the time required to get you there.
If you are already familiar with CircuiTikZ and write large schematics using it with ease, this script will only complicate your workflow. However, when you already use LTSpice for simulations, you might at least want to try it.
As with most automatically generated code, the output of the script is not as nicely formatted and structured as the result coming from a skilled human. More precisely, it uses absolute coordinates and it groups circuit elements by type and not by semantics. This means that you will find the wires of a voltage divider in a different part of the document than the resistors. The visual result is the same, but the LaTeX code structure could be improved.
Due to the limited graphical capabilities of LTSpice schematic capture (which aims at simulation, not publication), there are limits of what can be achieved without editing the LaTeX output. Especially the grid of LTSpice is rather rough and therefore limits component placement but helps to achieve consistent looking schematics.
It also requires the use of non-free (as in freedom) software if that's an issue for you. Since IMHO LTSpice is one of the best free (as in beer) simulators around, this is unlikely to change in the future. It also has a very easy to parse, yet astonishingly powerful file format for symbols and schematics, which was the reason for choosing it as schematic entry.
Trademark and copyright notice: LTspice is a registered trademarks of Linear Technology Corporation, now part of Analog Devices, Inc. This script or its author are not in any way affiliated to these companies.
This script is provided as-is and without any warranty.
sym32a\latex_preamble.tex
, sym32a\latex_closing.tex
). The file sym32a\latex_ext.tex
is meant to be used to add additional CircuiTikZ symbols or to patch existing ones.[general]
with the entry default_gnd=circuiTikz\gnd
. Replace the string right to the = sign with the path and name of the .asy2tex gnd symbol file you want to use (without the extension), e.g. circuiTikz\ground
.Creating new symbols in LTspice works the same way as it does normally (see the Creating New Symbols section in the LTspice help file). There are no required extra steps involved. To have good visual equivalence between the spice circuit and the circuiTikz result, your pin spacing and proportions in your spice symbol compared to existing symbols should approximately match the ratio of the two circuiTikz symbols. To make debugging easier, you could try to position your origin at the center or at a pin, but this is not mandatory. Please consult one of the many good tutorials on how to create symbols in LTspice on how to do this. If you only use the new symbol to create circuiTikz graphics, you don't need to provide a valid spice model. If you want to be able to simulate the circuit as well, you should provide one.
asy2tex files are the ones that store information on how to convert any symbol found in the schematic to circuiTikZ code. To add a new symbol you need to do the following:
sym32a
. The relative location of the symbol file in the sym folder needs to be the same as in the LTspice sym folder. I.e., if your symbol resides in sym\someFolder\newSymbol.asy
in your LTspice lib, you need to place it e.g. at sym32\someFolder\newSymbol.asy
. lt2circuitikz needs the symbol to look up the pin names and positions.MyNewSymbol.asy
to sym32a\someFolder\
, you need to create a MyNewSymbol.asy2tex
text file in the same directory (thus at sym32a\someFolder\MyNewSymbol.asy2tex
). This is a plain ascii text file.ype <SymbolTypeHere>
exOrigin <x y rot mirror>
ymOrigin <x y rot mirror>
eginPinList
ndPinList
exElementName <texElementName>
eginTex
<CircuiTikzCodeToCreateSymbol>
ndTex
There are also more advanced fields available, see below.UniversalOpamp2.asy2tex
as examples. It creates a named node by using the #self.name# token and thus allows a unique reference to its pins. You can also use the capability to specify relative coordinates in circuiTikz to your advantage, e.g. when positioning labels.asy2tex files perform the conversion from *.asy symbols to LaTeX commands. A typical asy2tex file looks like this:
Type Node
TexOrigin 0 0 0 False
SymOrigin 2.836 0 0 False
BeginPinList
EndPinList
TexElementName op amp
BeginTex
\draw (#self.texx1#, #self.texy1#) node[#self.symbol.latexElementName#, xscale=##mirror_xscale_value##, rotate=##rotate##, ##options##] (#self.name#) {} (#self.name#)++(##labelmirrorx##*0.75, ##labelmirrory##*1.25) node {#self.name# #self.value#};
\draw (#V+:x1#,#V+:y1#) to [*short, -] (#self.name#.up); \draw (#V-:x1#,#V-:y1#) to [*short, -] (#self.name#.down); % supply
\draw (#In+:x1#,#In+:y1#) to [*short, -] (#self.name#.+); \draw (#In-:x1#,#In-:y1#) to [*short, -] (#self.name#.-); \draw (#OUT:x1#,#OUT:y1#) to [*short, -] (#self.name#.out); % in/out
EndTex
and contains the following elements:
ALIASFOR ..\res.asy2tex
Type
TexOrigin x1 y1 rotatedeg mirror
SymOrigin x1 y1 rotatedeg mirror
BeginPinList
PinNum x1 y1 rot length PinName
(not shown)EndPinList
BeginConversionKeyvals
key=val
EndConversionKeyvals
TexElementName texname
BeginTex
LaTeX code
#self.texx1#
, #self.texy1#
#PinName:x1#
#PinName:y1#
#PinName:junction#
#self.symbol.latexElementName#
#self.name#
#self.value#
#self.value2#
or ##options##
##rotate##
##mirror_invert##
, ##mirror_mirror##
, ##mirror_xscale##
##mirror_xscale_value##
, ##mirror_yscale_value##
##mirror_rot_xscale_value##
##mirror_rot_yscale_value##
##rotate__pmvalue##
##rotate__mpvalue##
##rotate__10value##
##rotate__01value##
##labelmirror##
#self.#
#self.symbol.#
#self.symbol.conversionKV.#
EndTex
The most common components have already been translated and reside in the sym* folders. You can tweak them as you like to produce the output you desire.