A simplified way to insert TikZ graphics into actual documents.

One of the observations I seem to be making is, that the technology-intensive part of the world is still using such tools as LaTeX, to typeset documents and vector-based graphics, such as Mathematical equations which I’ve included in previous parts of this blog, even though we know that GUI-based applications have existed for some time, that also typeset and do graphics…

I like ‘LaTeX’, even though I never learned the full syntax. LaTeX defines the document in a type of textual syntax. And there is even a system of TeX macros, which defines a sub-language called “TikZ”, that defines entire figures, sketches and plots, using a textual syntax.

Edit: Alternatively, there is a sub-language called “PSTricks”, which offers yet another way to do the same thing

And so a question which strikes me as important is, how to use these languages, even though I don’t know the syntax. And the answer is, ‘I use a GUI.’ That GUI mainly consists of the application ‘LyX’.

(Updated 3/07/2019, 17h55 … )

(As of 3/04/2019, 16h05 : )

Therefore, the easiest and fastest way for me to include any type of TikZ graphic into a document, is to Insert it into my LyX document, as a Child Document. What I also need to do, is add the following two lines to my Custom Preamble:




What I must also do is to go into ‘Document -> Settings -> Fonts’ (within LyX), and I must check off the box that says, “Use non-TeX fonts (via XeTeX/LuaTeX)”. And I must specify whichever set of fonts installed on my system, that can render whatever Unicode characters may be present within the TikZ figure. The figure I gave for demonstration purposes, just happened to include a degree sign, which has Extended-ASCII character-code 176 in decimal (:1), and which the default compiler for LaTeX, that being ‘pdflatex’, screamed bloody murder about. 176 is greater than 127 and is therefore not a 7-bit ASCII character.

And then, because I have done all that, LyX no longer shows me the option to compile anything using ‘pdflatex’. I now get to compile my document using ‘XeTeX’, which works without any further issues.

I have uploaded a 10-minute screen-cast, in which I give a walk-through about just how simple the procedure can be, once the user knows the solution to such problems – as a degree-sign:


N.B.  One friend of mine actually asked me about this exercise:

“Isn’t the whole purpose of LaTeX to be able to print many exotic characters? Why does one Unicode character pose a problem?”

The original purpose behind LaTeX was, that all special characters be denoted by ASCII-character sequences, for which purpose LaTeX has its specific syntax. But the problem I dealt with above was, the imported .TEX-File did have a Unicode character instead.

When XeTeX was invented, its initial purpose was not so much, to allow Unicode to be put directly, as it was, to give access to all the system-installed fonts.

1:)  Actually, there exists more than one version of the little, super-scripted circle, that some of us just take to be a degree sign. Many of them have Unicode values much greater than 176 in decimal. The degree sign I dealt with may have been one such example.



(Update 3/05/2019, 14h30 : )

The problem which I addressed in the video above should not be confused with the question, of How somebody would compose a Chinese LaTeX Document, from directly within LyX.

In order to complete this latter task, the best solution is firstly, to have the following packages installed, that will bring Chinese to the collection of LaTeX Fonts (but not every Linux-package that matches the following patterns):




And then, it will be possible just to point-and-click on the Chinese Language, as shown below, within ‘Document -> Settings -> Language':



Doing so, will avoid having to set any of the non-default parameters in the following screen:



To the best of my knowledge, a LaTeX document will result, that can also be compiled using ‘pdflatex’. The user would just need to use whatever input method he usually does, to type in Chinese. (:2)

There is a reason however, why the above approach may not work, if a random .TEX-File has been included into the document. The .TEX-File in question did not have a preamble, so that it could not specify which character set it was using. And, there could be one Unicode character, which additionally does not belong to the Chinese character set, which would usually be specified by ASCII-encoded LaTeX syntax, but which has been put as a Unicode character.

Specifically in this case, access needs to be granted to the system-installed fonts, and the font chosen needs to be one, in which that one character just happens to have a defined shape. If in addition the user cannot recognize which character that is, then the way I showed in the video, remains the best way. And then it also follows, that the XeTeX compiler needs to be used, to output a PDF.

(Update 3/05/2019, 18h20 : )

There is another way to solve the problem, which my YouTube video describes. Even when using the ‘pdflatex’ program, it’s possible actually to prevent Unicode characters as such, from reaching the LaTeX compiler.

Thus, because I know what keyboard-sequence I was using, before I even generated the exported ‘TikZ’ graphic, I am able to determine that the actual Unicode representation of the degree symbol, in hexadecimal, is


Based on that, If I create a new LyX document, I can instead go into ‘Document -> Settings -> Language’, and I can select ‘utf8′ as my input encoding, even though my language could still be English. Then, I can go into ‘Document -> Settings -> LaTeX Preamble’, and I can make the following my new preamble:




This will replace each identified Unicode symbol with a proper, encoded LaTeX symbol. The advantage will be greater compatibility with different LaTeX formats, and not just with XeTeX. ‘pdflatex’ is now able to generate the correct PDF.

The reasons I did not explain this option in my video, would have been the need to explain as well, how to find the Hex value of a given Unicode symbol, plus the fact that only one character will be substituted in the preamble, at a time.


(Update 3/05/2019, 21h35 : )

The reader may be curious to know, how he can discover the Unicode values of keyboard-sequences, which he has made using the Compose Key, using his Linux-computer. The answer requires that the package ‘uni2ascii’ be installed. Given that, the user can edit a text file, let’s say named ‘degree.txt’, using any form of editor that accepts UTF-8 as its input encoding, and, using the Compose Key while editing. Most Linux text editors, including ‘KWrite’ and ‘vim’, do. Then, once the file has been saved and the editor quit, the user can run the following command on the text file:


$ uni2ascii -a H degree.txt


What will be output is a view of the text file, as if it was going to be encoded for use with HTML, but with hexadecimal escape-codes in particular, for all the Unicode characters. Other HTML special characters will not be escaped.

(Update 3/06/2019, 6h40 : )

If the reader wishes to format the entire Text-File as HTML, then one way to do so would be, additionally to make sure that the package ‘enscript’ is installed, and then to give the command:


$ enscript -Bhq -w html -o - degree.txt | uni2ascii -a H -q



(Update 3/06/2019, 7h35 : )

Some sketching applications – notably “Inkscape” – do not offer to export their graphics as ‘TikZ’, but rather as ‘PSTRicks’, which could cause some people confusion because both file-name extensions will be ‘.TEX’. If the user knows he has a ‘PSTricks’ file, then he can change his LaTeX preamble to:




One important fact to bear in mind about ‘PSTricks’ would be, that it requires output to .PS Format, for which reason to use the regular ‘pdflatex’ program will not work, and, XeTeX may not work either. Yet, when using LyX to set up the document, the option exists to go into ‘File -> Export -> PDF (ps2pdf)’. What this output option does is, first to use Plain LaTeX to generate a DVI and then a PS File, and then to use an added command to convert the PS File to a PDF.


(Update 3/07/2019, 17h55 : )

2: )

The way in which Language settings work with an up-to-date version of ‘pdflatex’ may require some elaboration. It’s possible to set an ‘Input Encoding’ which has non-ASCII, or even, UTF-8 encoding, which is also accessible directly from the LyX GUI, and which is ‘pdflatex’ -compatible as stated. But what the reader may want to take note of, is that he would always be enabling a subset of UTF-8, or otherwise, one of the Extended ASCII Tables.

UTF-8 is just a way of compressing Unicode into pieces that can be as short as 8 bits, and Unicode spans the entire range of internationally-defined symbols, including all supported languages. What the Input Encoding will want to do is select one language, which would also need to be installed within one of the LaTeX language packages.

Yet, within seconds in my exercise above, an example was at-hand of a Unicode character which did not belong to any one language per se: That Degree Sign. Not only that, but this character had a defined appearance in a plain font: ‘DejaVu’. And so for this I recommended that the default Input Encoding be deactivated, and that XeTeX be used instead.

When XeTeX was invented, this was a reflection on the practical absurdity of having to install language-packages to traditional LaTeX, when a modern PC already has multilingual support.



Print Friendly, PDF & Email

2 thoughts on “A simplified way to insert TikZ graphics into actual documents.”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>