Graphics, Figures & TablesDefinition of Variables for later Use

Information and discussion about graphics, figures & tables in LaTeX documents.
LaTeX Beginner
Posts: 30
Joined: Tue Sep 11, 2012 2:55 pm

Definition of Variables for later Use

Post by LaTeX Beginner »

Hi everybody! :D

Like my username says, I'm new using LaTeX and, of course, I'm new at this forum. This is my first post, and I would to ask you some help, because I really need it... :)

Before nothing, I wish that you apologize me if my english is not good... :D

Well, I'm trying to represent some parameters of an antenna in a radiation pattern. In this way, at first I've modified a example of radiation pattern made in LaTeX that I've found on the Internet. If I compile this, I get this radiation pattern, but the problem is that it appears moved to the right of the document, and I'm not able to center it. Here I post the code that I used to obtain the radiation pattern:

Code: Select all

\documentclass[11pt,a4paper,spanish]{article}

\usepackage{babel}
\usepackage[latin1]{inputenc}
\usepackage{graphicx}
\usepackage[dvips,final]{epsfig}
\usepackage{gnuplottex}
\usepackage{diagmac}
%\usepackage[miktex]{gnuplottex}
%\usepackage{gnuplot-lua-tikz}
\usepackage{tikz}
\usetikzlibrary{through}
\usepackage{verbatim}

\begin{document}

\begin{center}

\begin{tikzpicture}[scale=4.5]

\coordinate (a) at (1.5,0);
\coordinate (c) at (0,0);
\node [draw,line width=1pt,circle through=(a),
			label=left:$180^\circ$,
			label=right:$0^\circ$,
			%label=15:$15^\circ$,			
			label=30:$30^\circ$,
			%label=45:$45^\circ$,
			label=60:$60^\circ$,
			%label=75:$75^\circ$,
			label=90:$90^\circ$,
			%label=105:$105^\circ$,
			label=120:$120^\circ$,
			%label=135:$135^\circ$,
			label=150:$150^\circ$,
			%label=165:$165^\circ$,
			label=180:$180^\circ$,
			label=210:$210^\circ$,
			label=240:$240^\circ$,
			label=270:$270^\circ$,
			label=300:$300^\circ$,
			label=330:$330^\circ$] at (c) {};
\draw[line width=1pt,dashed] (0,0) circle (1.5/2);
\draw[line width=1pt,dashed] (0,0) circle (1.5/4);
\draw[line width=1pt,dashed] (0,0) circle (1.5*3/4);
\draw[line width=1pt,dashed] (0,0) -- (10:1.5);
\draw[line width=1pt,dashed] (0,0) -- (20:1.5);
\draw[line width=1pt,dashed] (0,0) -- (30:1.5);
\draw[line width=1pt,dashed] (0,0) -- (40:1.5);
\draw[line width=1pt,dashed] (0,0) -- (50:1.5);
\draw[line width=1pt,dashed] (0,0) -- (60:1.5);
\draw[line width=1pt,dashed] (0,0) -- (70:1.5);
\draw[line width=1pt,dashed] (0,0) -- (80:1.5);
\draw[line width=1pt,dashed] (0,0) -- (100:1.5);
\draw[line width=1pt,dashed] (0,0) -- (110:1.5);
\draw[line width=1pt,dashed] (0,0) -- (120:1.5);
\draw[line width=1pt,dashed] (0,0) -- (130:1.5);
\draw[line width=1pt,dashed] (0,0) -- (140:1.5);
\draw[line width=1pt,dashed] (0,0) -- (150:1.5);
\draw[line width=1pt,dashed] (0,0) -- (160:1.5);
\draw[line width=1pt,dashed] (0,0) -- (170:1.5);
\draw[line width=1pt,dashed] (0,0) -- (190:1.5);
\draw[line width=1pt,dashed] (0,0) -- (200:1.5);
\draw[line width=1pt,dashed] (0,0) -- (210:1.5);
\draw[line width=1pt,dashed] (0,0) -- (220:1.5);
\draw[line width=1pt,dashed] (0,0) -- (230:1.5);
\draw[line width=1pt,dashed] (0,0) -- (240:1.5);
\draw[line width=1pt,dashed] (0,0) -- (250:1.5);
\draw[line width=1pt,dashed] (0,0) -- (260:1.5);
\draw[line width=1pt,dashed] (0,0) -- (280:1.5);
\draw[line width=1pt,dashed] (0,0) -- (290:1.5);
\draw[line width=1pt,dashed] (0,0) -- (300:1.5);
\draw[line width=1pt,dashed] (0,0) -- (310:1.5);
\draw[line width=1pt,dashed] (0,0) -- (320:1.5);
\draw[line width=1pt,dashed] (0,0) -- (330:1.5);
\draw[line width=1pt,dashed] (0,0) -- (340:1.5);
\draw[line width=1pt,dashed] (0,0) -- (350:1.5);
\draw[line width=1pt] (180:1.5) -- (0:1.5);
\draw[line width=1pt] (270:1.5) -- (90:1.5);

\end{tikzpicture}
\end{center}
\end{document}
As you can see if you try this on LaTeX, the radiation pattern appears moved at the right of the document. Any idea to center it?

My objective is represent in the radiation pattern some parameters of an antenna. To do this, I must define some variables, which will be used inside other variables, but I have problems with that. I've tried to define a variable with the structure \def, but LaTeX gave me some errors... I've seen that \newcommand{\nameofvariable}{\information} could be useful. For example, if I have a variable that I call "N", how could I define it? Maybe in this way?

\newcommnad{\N}{}

And what is the information that I must to include in the second group of brackets? (Suppose that "N" is a numeric variable, that sometimes is defined like 2, or like 3, for example). This second group of brackets could not be taking into account?

Finally, I want to represent some parameter of the antenna in the radiation pattern above. This parameter is an equation, because uses the variables that I defined previously. For example, the parameter could be:

Parameter = (A/B)

How can I define this parameter, using again the \newcommand estructure? And how can I plot this on LaTeX? I've seen that the following structure is usually used:

\draw[colour...] plot[style] Parameter

Is this correct? How can I indicate that I want to represent this in the defined radiation pattern?

I'm sorry about the enormous size of the post! I expect for your help. :D

Regards.
Last edited by cgnieder on Tue Sep 11, 2012 8:56 pm, edited 3 times in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Definition of Variables for later Use

Post by localghost »

LateX Beginner wrote:[…] As you can see if you try this on LaTeX, the radiation pattern appears moved at the right of the document. Any idea to center it? […]
Modify the page dimensions by the geometry package.

I must confess that I don't really understand what you are after. But your code can be simplified quite a lot. Perhaps this already solves some issues.

Code: Select all

\documentclass[11pt,a4paper,spanish]{article}
\usepackage[T1]{fontenc}
\usepackage{selinput}   % semi-automatic selection of input encoding
\SelectInputMappings{   % by a list of selected glyphs
  ntilde={ñ},           % see: http://partners.adobe.com/public/developer/en/opentype/glyphlist.txt
  oacute={ó},
  Euro={€}
}
\usepackage{babel}
\usepackage{geometry}
\usepackage{tikz}
\usepackage{textcomp}

\begin{document}
  \begin{tikzpicture}
    \draw[thick] (-6,0) -- (6,0) (0,-6) -- (0,6);
    \foreach \r in {1.5,3,...,6}
      \draw (0,0) circle (\r);
    \foreach \r in {0,10,...,350}
      \draw[dashed] (0:0) -- (\r:6);
    \foreach \r in {0,30,...,330}
      \node at (\r:6.5) {\r\textdegree};
  \end{tikzpicture}
\end{document}
For details please have a look at the pgf/tikZ manual.

If this shall represent a polar coordinate system for a corresponding plot, you should take a look at the excellent pgfplots package.


Best regards and welcome to the board
Thorsten
Attachments
The resulting output.
The resulting output.
polar-grid.png (82.9 KiB) Viewed 16686 times
LaTeX Beginner
Posts: 30
Joined: Tue Sep 11, 2012 2:55 pm

Definition of Variables for later Use

Post by LaTeX Beginner »

Wow, thanks, you've really improved the code, also simplifying it! Thank you so much! :D

Like you say, I've heard that the package pgfplots is excellent to represent graphics, diagrams... but I don't know how to use it. Any example to obtain a polar coordinate system? Any manual of use?

The other question that I've done in the post is refered to the definition of variables, and the use of this variables inside another variables. For example, I want to define "A" and "B" like two different variables, which will be used inside other variables, like an equation. In this way, I define "A" and "B" using the structure \newcommand:

Code: Select all

\newcommand{\A}
\newcommand{\B}
Also, I want to define another variable, an equation, that I will name "C", and "C" depends on the two variables previously defined, "A" and "B", in the form:

Code: Select all

\newcommand{\C}
C=A/B
How can I define this equation, which will be used later to be represented in the radiation pattern?

If anyone has an idea, please, reply this. :)

I hope to explain myself better.

Regards.
Last edited by cgnieder on Tue Sep 11, 2012 8:57 pm, edited 1 time in total.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Definition of Variables for later Use

Post by localghost »

LaTeX Beginner wrote:[…] Like you say, I've heard that the package pgfplots is excellent to represent graphics, diagrams... but I don't know how to use it. Any example to obtain a polar coordinate system? Any manual of use? […]
If you just follow the link in my last reply, you will find everything you need. In case the package is already installed on your system, you have direct access to the manual on the command line.

Code: Select all

texdoc pgfplots
Another source for the manual is the TeXdoc Online service.
LaTeX Beginner wrote:[…] The other question that I've done in the post is refered to the definition of variables, and the use of this variables inside another variables. For example, I want to define "A" and "B" like two different variables, which will be used inside other variables, like an equation. In this way, I define "A" and "B" using the structure \newcommand:

\newcommand{\A}
\newcommand{\B}

Also, I want to define another variable, an equation, that I will name "C", and "C" depends on the two variables previously defined, "A" and "B", in the form:

\newcommand{\C}
C=A/B

How can I define this equation, which will be used later to be represented in the radiation pattern? […]
I think you should take a look at the mathematical engine of pgf. It is explained comprehensively in its manual.
LaTeX Beginner
Posts: 30
Joined: Tue Sep 11, 2012 2:55 pm

Re: Definition of Variables for later Use

Post by LaTeX Beginner »

Oh, thanks, I don't knew that I can access to the manual of each installed package..

Ok, I will study the pgfmanual. I hope this can solve my problems.

Thank you so much! :D
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Definition of Variables for later Use

Post by cgnieder »

Hi,

TeX is a pure macro expansion language so it does not know variables but only macros. TeX is also not the best choice for doing maths. Nevertheless there are some solutions which are astonishing when you think what you'd have to do on your own with plain TeX.

So let's see how you could create your three variables, TeX can either do maths with integers or with dimensions (=lengths). The integers are not quite satisfying as for example 2/3 would give 1. Better use dimensions and strip the unit

Code: Select all

\documentclass{article}
\begin{document}

\def\A{2}
\def\B{3}
\makeatletter
\def\C{\the\numexpr\A/\B\relax}
\makeatother

A: \A, B: \B, C: \C

\makeatletter
\def\C{\strip@pt\dimexpr\A pt/\B\relax}
\makeatother

A: \A, B: \B, C: \C

\end{document}
You can see that the precision is not very good. Another way would be to use the fp (floating point) package:

Code: Select all

\documentclass{article}
\usepackage{fp}
\begin{document}

\FPset\A{2}
\FPset\B{3}
\FPdiv\C\A\B

A: \A, B: \B, C: \C

\end{document}
Much better, isn't it? But there are still at least two further methods. One of them has been mentioned by Thorsten and might be the way to go as you are loading pgf already. I have not very much experience with it but it should be possible to gain more precision than the following example:

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fpu}
\begin{document}

\def\A{2}
\def\B{3}
\pgfmathparse{\A/\B}
\edef\C{\pgfmathresult}

A: \A, B: \B, C: \C

\pgfkeys{/pgf/number format/frac}
\pgfmathprintnumber\C

\end{document}
Last but definitely not least there is the l3fp module of the l3kernel. Here we pretend there are variables and functions (this is by design of the LaTeX3 programming layer expl3).

Code: Select all

\documentclass{article}
\usepackage{expl3,tikz}
\usetikzlibrary{fpu}
\begin{document}

\ExplSyntaxOn
% define variables
\fp_new:N \l_A_fp
\fp_new:N \l_B_fp
\fp_new:N \l_C_fp

% set variables:
\fp_set:Nn \l_A_fp { 2 }
\fp_set:Nn \l_B_fp { 3 }
\fp_set:Nn \l_C_fp { \l_A_fp / \l_B_fp }

% use variables:
A:~\fp_use:N \l_A_fp ,
B:~\fp_use:N \l_B_fp ,
C:~\fp_use:N \l_C_fp

% and for comparison with the previous example:
\par
\pgfkeys{/pgf/number~format/frac}
\pgfmathprintnumber{\fp_use:N \l_C_fp}
\ExplSyntaxOff

\end{document}
Regards
site moderator & package author
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Definition of Variables for later Use

Post by cgnieder »

One should probably mention that the l3fp module is not meant to be used on the user level but for package/class writers. Citing interface3:
But in all fairness, this module is mostly meant as an underlying tool for higher-level
commands. For example, one could provide a function to typeset nicely the result of
floating point computations.

Code: Select all

\usepackage{xparse, siunitx}
\ExplSyntaxOn
\NewDocumentCommand { \calcnum } { m }
{ \num { \fp_to_scientific:n {#1} } }
\ExplSyntaxOff
\calcnum { 2 pi * sin ( 2.3 ^ 5 ) }
What the best way is depends (as so often) on the actual use case…
site moderator & package author
LaTeX Beginner
Posts: 30
Joined: Tue Sep 11, 2012 2:55 pm

Re: Definition of Variables for later Use

Post by LaTeX Beginner »

Thank you so much, cgnieder, I really appreciate your help. :D

I tried the examples you posted, and I get the better results using the packages fp and l3kernel. pgf gave me some errors, I will investigate it...

I'm used to use MatLab, where is very easy to define maths... For this reason, LaTeX is making me a little crazy... :D

Regards.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Definition of Variables for later Use

Post by cgnieder »

LaTeX Beginner wrote:I'm used to use MatLab, where is very easy to define maths...
Well, MatLab is for doing maths and (La)TeX for typesetting so they have different goals.

If you study the pgf manual (pgfmanual, the tutorials are a very good start) you see that of course you can use “variables” in the graphics. Also, Thorsten already mentioned pgfplots which is very capable of creating plots using variables. Here is an example I used a while ago:

Code: Select all

\documentclass{scrartcl}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
 \def\Ka{10^(-4.75)}% the equilibrium constant
 \def\co{1}% the starting concentration
 \begin{axis}[ymode=log,domain=0:14]
  \addplot[red] { \co*10^(-x)/(\Ka+10^(-x)) };
  \addplot[blue] { \co*\Ka/(10^(-x)+\Ka) };
 \end{axis}
\end{tikzpicture}
haegg-diagramm.png
haegg-diagramm.png (8.71 KiB) Viewed 16665 times
Regards
site moderator & package author
LaTeX Beginner
Posts: 30
Joined: Tue Sep 11, 2012 2:55 pm

Re: Definition of Variables for later Use

Post by LaTeX Beginner »

This an example that could be very useful to me, serioulsy. Thank you so much, once again :D

I'm studying the pgfplots manual, and I'm able to represent already some functions in polar coordinates... When I'm capable to represent the function that I'm trying, I will post here the result, in order to thank the help that all you have given to me.

Thank you so much! :D
Post Reply