Informatics1-2017/Practice8

A MathWikiből
(Változatok közti eltérés)
Kkovacs (vitalap | szerkesztései)
(Új oldal, tartalma: „== Newcommand == * With the '''\newcommand''' command we can define new commands. These should be placed in the preamble (before '''\begin{document}''').. * We can thi…”)
Újabb szerkesztés →

A lap 2017. október 30., 06:38-kori változata

Tartalomjegyzék

Newcommand

  • With the \newcommand command we can define new commands. These should be placed in the preamble (before \begin{document})..
  • We can think of it as an abbreviation.
  • For example, if we write the set of real numbers a lot of times, we can do the following:
...
\newcommand{\R}{\mathbb{R}}
...
\begin{document}
\[
x^2 \geq 0 \quad \forall x\in\R
\]
\end{document}
  • With this newcommand if we write \R it gets substituted with \mathbb{R}.
  • The first parameter of newcommand is the name of the command (given by us), the second is the existing command (can be a set of commands) we wish to shorten.
  • We could do the same for the set of integers, etc.

Arguments

  • We can define complex commands, for example if we would like to make a command for a nice integral:
...
\newcommand{\myint}[2]{\int #1 \,\mathrm{d}#2}
...
\begin{document}
\[
\myint{x^2 \sin^2 x}{x} + \myint{x^2 \cos^2 x}{x} = \myint{x^2}{x}
\]
\end{document}
  • Here the optional parameter [2] tells latex that the command has two arguments (inputs).
  • We can specify the position of the arguments by #1, #2... Here the first parameter is the integrand, the second is the variable.
  • Ha már létezik olyan nevet akarunk adni a parancsunknak, ami már létezik, akkor hibaüzenetet fogunk kapni, például az elõzõ esetben, ha int-nek neveztem volna.
  • Ekkor is felül lehet definiálni a parancsot a \renewcommand paranccsal, bár ez az esetek többségében nem ajánlott.

Feladatok

Minta latex dokumentum keret:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[magyar]{babel}
 
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
 
\usepackage{tikz} % ez a rajzoló csomag, amit az óra második felében fogunk használni.
 
%Rosszul elválasztott szavak elválasztását tudjuk javítani
\hyphenation{ke-rék-pár-út}
 
\begin{document}
 
 
 
\end{document}

Halmazok

Definiáljuk a racionális, egész és természetes számok halmazát a példában látott módon. Majd ezeket próbáljuk is ki.

Vektor

Definiáljunk parancsot, melynek egy argumentuma van. A parancs tegyen egy jobbra nyilat az argumentum fölé az \overrightarrow{ }-val. Ezzel vektorokat tudunk majd kényelmesen írni. Nevezzük például vec-nek, majd miután lefordítottuk és rájöttünk, hogy ez már foglalt nevezzük át myvect-re. Majd írjuk le egy tetszõleges képletet vektorokkal.

Igazából annyira nem is rövidítette le a kódunkat ez az új parancs, hisz csak overrightarrow helyett myvect-et kell írni, ami csak 8 karakterrel kevesebb. Mégis hasznos, gondoljunk bele, ha megírtunk egy több oldalas dokumentumot, tele vektorokkal és hirtelen meggondoltuk magunkat és inkább aláhúzással szeretnénk jelölni a vektorokat. Ha a myvect-et használtuk a vektorokhoz, akkor elég csak a newcommand-ban megváltoztatni a jelölést, aláhúzás esetén \underline{ }-ra. Próbáljuk is ezt ki.

Személyes eszközök