Graphics, Figures & TablesProblems with tikZ

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
thadrien
Posts: 4
Joined: Wed Dec 22, 2010 9:51 pm

Problems with tikZ

Post by thadrien »

Hello,

I am currently trying to make a graph with tikZ, to include it in a LaTeX article. After spending hours, I managed to get something, but not as good as I would like. There is still some problems I don't manage to fix.

My LaTeX code is the following :

Code: Select all

\documentclass[a4paper]{article}

\usepackage{pgfplots}
\usepackage{units}
\pgfplotsset{compat=1.3}% <-- moves axis labels near ticklabels
                        % (respects tick label widths)

\begin{document}

  \begin{figure}
    \centering

    \begin{tikzpicture}

      \tikzset{
        every pin/.style={font=\tiny},
      }

      \begin{axis}[
        hide x axis,
        axis y line = left,
        ylabel = {\(F(J)\) \((\unit{cm^{-1}})\)},
        no markers,
        xmin = -0.5,
        xmax = 11,
        ymin = -30,
        ymax = 460
      ]

        % Energy lines
        \addplot [color=black] coordinates { (0,  0)      (2,  0)};     % F(J) = 0
        \addplot [color=black] coordinates { (0,  3.84)   (2,  3.84)};  % F(J) = 3.84
        \addplot [color=black] coordinates { (0, 11.73)   (2, 11.73)};  % F(J) = 11.73
        \addplot [color=black] coordinates { (0, 23.23)   (2, 23.23)};  % F(J) = 23.23
        \addplot [color=black] coordinates { (0, 38.63)   (2, 38.63)};  % F(J) = 38.63
        \addplot [color=black] coordinates { (0, 57.83)   (2, 57.83)};  % F(J) = 57.83
        \addplot [color=black] coordinates { (0, 80.93)   (2, 80.93)};  % F(J) = 80.93
        \addplot [color=black] coordinates { (0, 107.83)  (2, 107.83)}; % F(J) = 107.83
        \addplot [color=black] coordinates { (0, 138.63)  (2, 138.63)}; % F(J) = 138.63
        \addplot [color=black] coordinates { (0, 173.23)  (2, 173.23)}; % F(J) = 173.23
        \addplot [color=black] coordinates { (0, 211.63)  (2, 211.63)}; % F(J) = 211.63
        \addplot [color=black] coordinates { (0, 253.93)  (2, 253.93)}; % F(J) = 253.93
        \addplot [color=black] coordinates { (0, 300.03)  (2, 300.03)}; % F(J) = 300.03
        \addplot [color=black] coordinates { (0, 353.83)  (2, 353.83)}; % F(J) = 353.83
        \addplot [color=black] coordinates { (0, 407.63)  (2, 407.63)}; % F(J) = 407.63

        % Energy lines descriptions
        \node[coordinate,pin=-7:{ ~0~~~~~~~1~~~~~~~~~1.00~~~~~~~~~1.00~~~~3.84}] at (axis cs:2,0)      {}; % F(J) = 0
        \node[coordinate,pin=-3:{ ~1~~~~~~~3~~~~~~~~~0.981~~~~~~~~2.94~~~7.69}]  at (axis cs:2,3.84)   {}; % F(J) = 3.84
        \node[coordinate,pin=3: { 2~~~~~~~5~~~~~~~~~0.945~~~~~~~~4.73~~~11.5}]   at (axis cs:2,11.73)  {}; % F(J) = 11.73
        \node[coordinate,pin=3: { 3~~~~~~~7~~~~~~~~~0.893~~~~~~~~6.25~~~15.4}]   at (axis cs:2,23.23)  {}; % F(J) = 23.23
        \node[coordinate,pin=0: { 4~~~~~~~9~~~~~~~~~0.828~~~~~~~~7.45~~~19.2}]   at (axis cs:2,38.63)  {}; % F(J) = 38.63
        \node[coordinate,pin=0: { 5~~~~~~11~~~~~~~~0.754~~~~~~~~8.29~~~23.1}]    at (axis cs:2,57.83)  {}; % F(J) = 57.83
        \node[coordinate,pin=0: { 6~~~~~~13~~~~~~~~0.673~~~~~~~~8.75~~~26.9}]    at (axis cs:2,80.93)  {}; % F(J) = 80.93
        \node[coordinate,pin=0: { 7~~~~~~15~~~~~~~~0.590~~~~~~~~8.85~~~30.8}]    at (axis cs:2,107.83) {}; % F(J) = 107.83
        \node[coordinate,pin=0: { 8~~~~~~17~~~~~~~~0.507~~~~~~~~8.62~~~34.6}]    at (axis cs:2,138.63) {}; % F(J) = 138.63
        \node[coordinate,pin=0: { 9~~~~~~19~~~~~~~~0.428~~~~~~~~8.13~~~38.4}]    at (axis cs:2,173.23) {}; % F(J) = 173.23
        \node[coordinate,pin=0: {10~~~~~~21~~~~~~~~0.355~~~~~~~~7.46~~~42.3}]    at (axis cs:2,211.63) {}; % F(J) = 211.63
        \node[coordinate,pin=0: {11~~~~~~23~~~~~~~~0.288~~~~~~~~6.62~~~46.1}]    at (axis cs:2,253.93) {}; % F(J) = 253.93
        \node[coordinate,pin=0: {12~~~~~~25~~~~~~~~0.230~~~~~~~~5.75~~~50.0}]    at (axis cs:2,300.03) {}; % F(J) = 300.03
        \node[coordinate,pin=0: {13~~~~~~27~~~~~~~~0.180~~~~~~~~4.86~~~53.8}]    at (axis cs:2,353.83) {}; % F(J) = 353.83
        \node[coordinate,pin=0: {14~~~~~~29~~~~~~~~0.138~~~~~~~~4.00~~~57.7}]    at (axis cs:2,407.63) {}; % F(J) = 407.63

        % Legend
        \node[coordinate,pin=0:{{~\(J\)~~~\(2J+1\)~~~\(\exp(\frac{-E_r}{kT})\)~~~\(\frac{N_J}{N_0}\)~~~\(\tilde{v} (\unit{cm})\)}} ]  at (axis cs:2,440) {};

      \end{axis}

    \end{tikzpicture}

    \caption{Rotational term values \(F(J)\) (horizontal lines), relative
      populations \(\nicefrac{N_j}{N_0}\) and transition wavenumbers
      \(\tilde{v}\) of CO [Ref XXX].}

  \end{figure}

\end{document}


I would like to make this schematic :
energylevelsscan.png
energylevelsscan.png (51.15 KiB) Viewed 4463 times
But with a better layout than the "scan of a scan" version.

The problems I have are :

1) Texts are not aligned.
2) Left to the "table" legend, there is a line I don't want.

May anyone help me ?
Thanks a lot !
Hadrien Theveneau


Edit by localghost: Preferably no external links (see Board Rules). Attachments go onto the forum server where possible.
Last edited by thadrien on Mon Dec 27, 2010 9:35 am, edited 1 time in total.

Recommended reading 2024:

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

NEW: TikZ book now 40% off at Amazon.com for a short time.

johnjamessmith0
Posts: 37
Joined: Sun Mar 29, 2009 1:41 am

Problems with tikZ

Post by johnjamessmith0 »

I'm learning to use TikZ too. I managed to come up with a solution with rather ugly parts in the code, but I hope the output will do. It isn't very similar to your original code, sorry. Perhaps the most unsatisfactory part is the manual alignment of the column headers. Also, if you make the font size smaller, you'll have to make a number of other adjustments to fix the appearance.

I would love it if someone could improve upon this ugly code.

Code: Select all

\documentclass{article}

\usepackage{ifthen}
\usepackage{tikz}
	\usetikzlibrary{matrix}
\usepackage{units}

\begin{document}

  \begin{figure}
    \centering

		
		\newlength{\MagicalVerticalUnit}					% unit for scaling
		\setlength{\MagicalVerticalUnit}{0.03cm}         % change this value to vertically compress everything (font size can be changed at line 57)
		\newlength{\AxisSep}                      % horizontal distance between the vertical axis and the horizontal bars
		\setlength{\AxisSep}{12pt}
		\newlength{\BarLength}                    % length of the horizontal bars
		\setlength{\BarLength}{70pt}
		
		%% the values of F(J), stored in an array for future use
		\newcommand{\FofJs}{{0,3.84,11.73,23.23,38.63,57.83,80.93,107.83,138.63,173.23,211.63,253.93,300.03,353.83,407.63}}
		\newcommand{\TippityTop}{410}

    \begin{tikzpicture}[y=\MagicalVerticalUnit]

			%% the axis
			\draw (-\AxisSep,0) -- ++(0,\TippityTop) node [above] {\(F(J)\) \((\unit{cm^{-1}})\)};
			\foreach \x in {0,100,...,\TippityTop}
			{
				\draw (-\AxisSep,\x) node[left] {\x} -- ++(2pt,0);
			}
			
			%% the horizontal bars and the up-arrows
			\foreach \x in {0,1,...,14}
			{
				\draw[thick] (0,\FofJs[\x]) -- ++(\BarLength,0);
				\ifthenelse{ \equal{\x}{14} }              % omit arrow for top bar
					{}
					{\draw[->] (\x ex,\FofJs[\x]) -- (\x ex,\FofJs[\x+1]);}
			};
			
			%% stuff used to compute the distance between the rows of data
			\newcounter{level}
			\setcounter{level}{14}
			\newcommand{\diffy}%
			{ \\%
				[ { % in a tikz matrix, \\[<length>] starts a new row with additional vertical separation <length>
						max( (\FofJs[\thelevel] - \FofJs[\thelevel-1]) * \MagicalVerticalUnit ,%
								 10pt )             % to prevent the crowding, the minimum row baselineskip is set to 10pt; this may be changed with smaller font sizes
				} ] %
				\addtocounter{level}{-1} %
			}
			
			%% the data table
			\matrix [matrix of math nodes,
							 every node/.style={anchor=east},   % to use tiny text, use style={anchor=east,font=\tiny} here
							 matrix anchor=my top.west,
							 column sep=2ex,
							 row sep={between origins,0}]
			at (\BarLength+5pt,\FofJs[14])
			{
				\node[xshift=-1pt] {$J$};                     &    % This is
				\node[xshift=9pt]  {$2J+1$};                  &    % the first
				\node[xshift=10pt] {$\exp(\frac{-E_r}{kT})$}; &    % row of
				\node[xshift=-1pt] {$\frac{N_J}{N_0}$};       &    % the table.
				\node[xshift=5pt]  {$\tilde{v} (\unit{cm})$};      % The centering was done manually.
																		 \\[20pt]
				\node(my top) {14};  & 29 & 0.138 & 4.00 & 57.7\diffy    % if you leave a blank line between these line and the previous, you get errors (idky)
				13 & 27 & 0.180 & 4.86 & 53.8\diffy
				12 & 25 & 0.230 & 5.75 & 50.0\diffy
				11 & 23 & 0.288 & 6.62 & 46.1\diffy
				10 & 21 & 0.355 & 7.46 & 42.3\diffy
				 9 & 19 & 0.428 & 8.13 & 38.4\diffy
				 8 & 17 & 0.507 & 8.62 & 34.6\diffy        % for some reason if I put a space before each \diffy, the 3.84 in the last row gets messed up
				 7 & 15 & 0.590 & 8.85 & 30.8\diffy
				 6 & 13 & 0.673 & 8.75 & 26.9\diffy
				 5 & 11 & 0.754 & 8.29 & 23.1\diffy
				 4 &  9 & 0.828 & 7.45 & 19.2\diffy
				 3 &  7 & 0.893 & 6.25 & 15.4\diffy
				 2 &  5 & 0.945 & 4.73 & 11.5\diffy
				 1 &  3 & 0.981 & 2.94 & 7.69\diffy
				 0 &  1 & 1.00\hphantom{0} & 1.00 & 3.84\\
			};
		
			
    \end{tikzpicture}

    \caption{Rotational term values \(F(J)\) (horizontal lines), relative
      populations \(\nicefrac{N_j}{N_0}\) and transition wavenumbers
      \(\tilde{v}\) of CO [Ref XXX].}

  \end{figure}

\end{document}


thadrien
Posts: 4
Joined: Wed Dec 22, 2010 9:51 pm

Re: Problems with tikZ

Post by thadrien »

Hello,

Thanks a lot for your reply ! The output is pretty good, and I can include it in my paper.

Just for my personal knoledgde, does someone here know how to align automatically the columns ? Here, the align is done manually.

Happy Christmas and Happy New Year !

P.S : Of course, I will include you and all people who help me in the aknoledgements section of my paper.
johnjamessmith0
Posts: 37
Joined: Sun Mar 29, 2009 1:41 am

Problems with tikZ

Post by johnjamessmith0 »

I'm glad the solution works for you. I should add that you might want to put a green checkmark on your OP, as described in http://www.latex-community.org/forum/vi ... =37&t=5838, section 3, before localghost comes around. ;)
thadrien wrote: Just for my personal knoledgde, does someone here know how to align automatically the columns ? Here, the align is done manually.
Do note, however, that the columns are aligned automatically by the tikz matrix; it was just the centering of the column heads that head to be done manually.
thadrien
Posts: 4
Joined: Wed Dec 22, 2010 9:51 pm

Re: Problems with tikZ

Post by thadrien »

Ok, I just see that. Thanks a lot for your help !
Post Reply