📜 ⬆️ ⬇️

Creating your own sophisticated styles for LaTeX

Background


If you regularly create uniform documents in TeX, then creating your own style can significantly speed up the work.

My regular documents are creating leaflets with tasks for schoolchildren. Finished they look like this: PNG or PDF (and also so , so or so )
Almost all the design here (as well as a separate table for the results) is created automatically.
(if it is necessary for someone, I can issue a package with documentation and examples of use)

I would like to share different “tricks” when creating my own style.
If you are not at all in the subject of creating your teams, then it is best to read Lviv or something like that.
')

Commands with Russian letters (as well as how to use Russian in formulas and how to add Russian to pdf)



It is often convenient to create a team consisting of Russian letters. For example, to write
  \ ,  ... 


The barrier to creating such commands is that TeX, by default, considers Russian letters not quite letters (they have the wrong catcode (see Knut)).
To explain your desire to TeH, you need to fulfill either a large but understandable
 \catcode156=11\catcode201=11\catcode212=11\catcode223=11\catcode234=11\catcode245=11 \catcode188=11\catcode202=11\catcode213=11\catcode224=11\catcode235=11\catcode246=11 \catcode192=11\catcode203=11\catcode214=11\catcode225=11\catcode236=11\catcode247=11 \catcode193=11\catcode204=11\catcode215=11\catcode226=11\catcode237=11\catcode248=11 \catcode194=11\catcode205=11\catcode216=11\catcode227=11\catcode238=11\catcode249=11 \catcode195=11\catcode206=11\catcode217=11\catcode228=11\catcode239=11\catcode250=11 \catcode196=11\catcode207=11\catcode218=11\catcode229=11\catcode240=11\catcode251=11 \catcode197=11\catcode208=11\catcode219=11\catcode230=11\catcode241=11\catcode252=11 \catcode198=11\catcode209=11\catcode220=11\catcode231=11\catcode242=11\catcode253=11 \catcode199=11\catcode210=11\catcode221=11\catcode232=11\catcode243=11\catcode254=11 \catcode200=11\catcode211=11\catcode222=11\catcode233=11\catcode244=11\catcode255=11 

either more compact, but not quite obvious (taken from the russlh.sty style)
 \def\reserved@a#1#2{\@tempcnta#1\relax\@tempcntb#2\relax\reserved@b} \def\reserved@b{\ifnum\@tempcnta>\@tempcntb\else\reserved@c\@tempcnta\advance\@tempcnta\@ne\expandafter\reserved@b\fi} \def\reserved@c#1{\catcode#1=11\relax} \reserved@a{192}{223} % - \reserved@a{224}{255} % - \catcode156=11 %  \catcode188=11 %  


After that, problems with Russian letters in the teams will disappear.

Russian language and formulas

If you want to use Russian letters as Russian letters in formulas, then the mathtext package will save.

Russian language and PDF

If you want to get normal Russian letters when searching in ready-made pdf, then the cmap package will help. However, there is still a better solution: you can click the glyphtounicode.tex file with unicod codes for a significant part of the characters, then in the finished pdf you can search for (and save-paste) Greek letters and many mathematical icons.

Difficult settings for environments and styles (as well as how to set an arbitrary size and leading)



How to make your environment or your style so that you can specify in the parameters:
 [Par1=Value1, Par2=Value2, dump] 
?

It would be very convenient in many cases.

The answer gives the package keyval.sty.

What to do

First we connect the style:
 \usepackage{keyval} 


Then we define how to handle the keys:
 \define@key {  } {  } [  ] (   ,        ) {  } 


Now we call the command that parses the text, for each correct variable it will execute what was written in the corresponding \ define @ key
 \setkeys {  } {,   } 


An example for the environment


So, for example, we want to create environments in which the spaces between lines are specified explicitly, and also the font size is selected. (Thanks to Yandex.Ref for giving the text)

Code:
 \documentclass[12pt]{article} \usepackage[textwidth=5cm]{geometry} \usepackage[T2A]{fontenc} \usepackage[cp1251]{inputenc} \usepackage[russian]{babel} \usepackage{keyval} \sloppy \makeatletter \newlength{\wid} %  ---   \newtoks{\fnt} %  ---   \newlength{\skp} %  ---    \define@key{key}{font}[12]{\fnt={#1}} %   font,     (   ,  12) \define@key{key}{skip}{\skp=#1} \define@key{key}{width}{\wid=#1} \newenvironment{test}[1]% %      {\setkeys{key}{#1}% %   \begin{minipage}{\wid} %     \fontsize{\the\fnt}{\skp}\selectfont %       }% {\par\end{minipage}} %   \makeatother % @    \begin{document} \begin{test}{font=6,skip=14pt,width=5cm}    .  , ,      ,  .  , ,    ,     .    . \end{test} \bigskip \begin{test}{width=7cm,skip=13pt,font}    .  , ,      ,  .  , ,    ,     .    . \end{test} \end{document} 

Result:
image

(read about \ fontsize and \ selectfont in Knut or at Lvov)

Difficult style options

Now it’s a more difficult task to make such parameters the style.
To solve the problem, we had to study how the geometry.sty style works.

Everything is very similar here, but you need to add some code that will force the parameters at the very beginning of the style. Let's give an example, it will be quite enough:

An example (and also how to make the mag command work correctly with the fields of the document)


test.sty

 \ProvidesPackage{test.sty} \RequirePackage[T2A]{fontenc} \RequirePackage[cp1251]{inputenc} \RequirePackage[russian]{babel} \RequirePackage{keyval} %     ,     \define@key{key}{mag}{\mag#1} % , ,     \let\@tempa\@empty% \edef\@tempa{\@ptionlist{\@currname.\@currext}}% \@for\CurrentOption:=\@classoptionslist\do{% \@ifundefined{KV@#2@\CurrentOption}{}{\edef\@tempa{\@tempa,\CurrentOption,}}}% \edef\@tempa{\noexpand\setkeys{key}{\@tempa}}% \@tempa% \AtEndOfPackage{\let\@unprocessedoptions\relax}% %    ,       dvi,   pdf. %   ,  dvi  pdf    , %       ( true),   --- . % -   . \global\setlength\paperheight {297truemm}% \global\setlength\paperwidth {210truemm}% \global\setlength\pdfpageheight{297truemm}% \global\setlength\pdfpagewidth {210truemm}% \global\setlength\textheight {260truemm}% \global\setlength\textwidth {180truemm}% \global\setlength\hoffset {-1truein}% \global\setlength\voffset {-1truein}% \global\setlength\pdfhorigin {1truein}% \global\setlength\pdfvorigin {1truein}% \global\setlength\oddsidemargin{15truemm}% \global\setlength\topmargin {5truemm}% \global\setlength\leftskip {0truemm}% \global\setlength\rightskip {0truemm}% 

test.tex

 \documentclass[12pt]{article} \usepackage[mag=2000]{test} \begin{document} ! \end{document} 

(The mag command, which allows you to arbitrarily scale the document is very good. But it needs to be called as soon as possible. Many glitches arise from it: for example, 1cm or 1in scaling affects, but 1truecm or 1truein does not. Therefore, you should use true in all fields. - units, and in all other places - regular ones. Because of this, the geometry package with mag does not work well. In addition, different sizes are used for page sizes (and shifts) in pdf and dvi (for example, paperheight and pdfpageheight), which leads to even more problems. The given code takes them into account)

What else?



For more than 8 years of using TeH and creating my own style, I have accumulated quite a lot of different tricks, tricks, and generally nontrivial moments of poorly lit literature.
If there is interest, then it can also be illuminated.

For example:
  1. Creating portable builds and configuring WinEdt (my build on torrents)
  2. Configuring WinEdt for direct and reverse search immediately in PDF (you can forget about dvi) (on Habré we already wrote a little about this
  3. Creation of commands that "analyze" the structure of the document on the fly - for example, automatic creation of complex conduits, and also write some data to third-party files on the fly (for example, we create a special xml on the fly, which is used further for electronic conduit)
  4. Processing and executing perl-, python-, metapost-, or even any pieces of third-party code on the fly
  5. As a result, by inaccurately compiling someone else's document, you can, say, delete some data, run a virus program, etc. etc. (example ;-)
  6. Creating and adding drawings, graphs, etc. right in TeX, in metapost, IPE, GeoAlgebra, etc. (about this, too, was one , two , three , but the topic is immense.

Source: https://habr.com/ru/post/120401/


All Articles