Showing posts with label LaTeX. Show all posts
Showing posts with label LaTeX. Show all posts

Tuesday, May 11, 2021

How to inlude R code and output, and writing LaTeX equations on Blogger

Including R code and output:

1) Copy code and/or output from R

2) Go to http://hilite.me/

3) Paste code and/or output in Source Code panel 

4) Select 'S' in language drop-down menu

5) Tick ''Line numbers' option if necessary

6) Click 'Highlight!'

7) Copy result from the HTML panel.

8) Put Blogger post on HTML view (upper-left, drop-down arrow next to pencil) and paste. 

9) Go back to writing view( upper-left, drop-down arrow next to pencil) and see result.

 

Writing LaTeX equations:

When writing a post on Blogger, click the drop-down arrow next to the pencil symbol in the top-left and select HTML view.

Copy-paste the following lines to the very start of the HTML code:

<head><script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js" type="text/javascript">
MathJax.Hub.Config({
 extensions: ["tex2jax.js","TeX/AMSmath.js","TeX/AMSsymbols.js"],
 jax: ["input/TeX", "output/HTML-CSS"],
 tex2jax: {
     inlineMath: [ ['$','$'], ["\\(","\\)"] ],
     displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
 },
 "HTML-CSS": { availableFonts: ["TeX"] }
});
</script></head>  

Now if you can write equations within the usual dollar signs. That is, typing:

$\alpha$

will read $\alpha$ (but not while you are writing the post; click 'Example' in the top-right to check whether it works as expected when viewing the post).

Friday, January 10, 2014

Favorite online LaTeX editors

Online realtime latex editors
 
http://www.codecogs.com/latex/eqneditor.php can output to gif, or gives HTML or URL, among other options. I especially like the functionality which allows you to use:

http://latex.codecogs.com/gif.latex?[any funcion].

Try for example:

http://latex.codecogs.com/gif.latex?\int_0^\infty e^{-x^2} dx


Oh, wait! www.texify.com offers the same funtionality, and the website is way cleaner!

For example:

http://texify.com/[any latex code]

http://texify.com/?$int_0^\infty e^{-x^2} dx$



Online .xlsx to latex table converter:

I really love this one! Just ddrag and drop your spreadsheet to the following URL, and it provides the code for a latex table:

http://ericwood.org/excel2latex/

Accented characters in LaTeX and BibTex

Accented characters in LaTeX 
 
  
Accented characters in BibTex 
 
 
 
 
 
 
 

Thursday, December 19, 2013

Tikz: simple decision tree (or CART tree) example



I created the above diagram of a decision tree using the tikz package for LaTeX. It took me a while to find a simple, clear way to produce a decision tree. This is the code I used:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\newdimen\nodeDist
\nodeDist=35mm
\begin{document}

\begin{tikzpicture}[
    node/.style={%
      draw,
      rectangle,
    },
  ]

    \node [node] (A) {IDS $>$ 13.5?};
    \path (A) ++(-135:\nodeDist) node [node] (B) {exit 1};
    \path (A) ++(-45:\nodeDist) node [node] (C) {LCIanx $>$ 0.3604?};
    \path (C) ++(-135:\nodeDist) node [node] (D) {exit 2};
    \path (C) ++(-45:\nodeDist) node [node] (E) {exit 3};

    \draw (A) -- (B) node [left,pos=0.25] {no}(A);
    \draw (A) -- (C) node [right,pos=0.25] {yes}(A);
    \draw (C) -- (D) node [left,pos=0.25] {no}(A);
    \draw (C) -- (E) node [right,pos=0.25] {yes}(A);
\end{tikzpicture}

\end{document}

Friday, September 27, 2013

Adjusting size of nodes and text labels in tikz (LaTeX)

The following picture is created with tikz, using the same node sizes and text label sizes for every node:


\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\begin{document}

\begin{tikzpicture}[auto,node distance=.5cm,
    latent/.style={circle,draw,very thick,inner sep=0pt,minimum size=30mm,align=center},
    manifest/.style={rectangle,draw,very thick,inner sep=0pt,minimum width=45mm,minimum height=10mm},
    paths/.style={->, ultra thick, >=stealth'},
]

% Define observed variables
\node [manifest] (things) at (0,0) {things};

% Define latent variables
\node [latent] (all) [left=3.5cm of things] {All};
\node [latent] (random) [above right=3.5cm of things] {random};


% Draw paths from latent to observed variables
\draw [paths] (all) to node { } (things);
\draw [paths] (random) to node { } (things);

\end{tikzpicture}
\end{document}




However, I didn't like the node sizes,  so I changed them using the 'scale' argument within the 'node' command:



\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\begin{document}

\begin{tikzpicture}[auto,node distance=.5cm,
    latent/.style={circle,draw,very thick,inner sep=0pt,minimum size=30mm,align=center},
    manifest/.style={rectangle,draw,very thick,inner sep=0pt,minimum width=45mm,minimum height=10mm},
    paths/.style={->, ultra thick, >=stealth'},
]

% Define observed variables
\node [manifest, scale=.75] (things) at (0,0) {things};

% Define latent variables
\node [latent] (all) [left=3.5cm of things] {All};
\node [latent, scale=.5] (random) [above right=3.5cm of things] {random};


% Draw paths from latent to observed variables
\draw [paths] (all) to node { } (things);
\draw [paths] (random) to node { } (things);

\end{tikzpicture}
\end{document}




The scale command changes the size of the node, as well as the size of the text label. So, you may want to change the size of the text labels only. You can do this by using one of the following LaTeX text size commands:




\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\begin{document}

\begin{tikzpicture}[auto,node distance=.5cm,
    latent/.style={circle,draw,very thick,inner sep=0pt,minimum size=30mm,align=center},
    manifest/.style={rectangle,draw,very thick,inner sep=0pt,minimum width=45mm,minimum height=10mm},
    paths/.style={->, ultra thick, >=stealth'},
]

% Define observed variables
\node [manifest, scale=.75] (things) at (0,0) {\Large things};

% Define latent variables
\node [latent] (all) [left=3.5cm of things] {\large All};
\node [latent, scale=.5] (random) [above right=3.5cm of things] {\huge random};


% Draw paths from latent to observed variables
\draw [paths] (all) to node { } (things);
\draw [paths] (random) to node { } (things);

\end{tikzpicture}
\end{document}




Note that you can always change font size locally in LaTeX documents by using one of the following commands:

\tiny
\scriptsize
\footnotesize
\small
\normalsize
\large
\Large
\LARGE
\huge
\Huge 

Friday, September 20, 2013

I think tikz iz fantaztic! (software for drawing SEM models)

I have been trying to produce my own diagrams for structural equation models and path diagrams. Today, I produced the following diagram using the tikz package in LaTeX:


Here is the code for creating the diagram (please note: it works using 'pdf texify', not using 'texify', not sure about the rest):


\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\newcommand{\at}{\makeatletter @\makeatother}
\begin{document}
\begin{tikzpicture}[auto,node distance=.5cm,
    latent/.style={circle,draw,very thick,inner sep=0pt,minimum size=30mm,align=center},
    manifest/.style={rectangle,draw,very thick,inner sep=0pt,minimum width=45mm,minimum height=10mm},
    paths/.style={->, ultra thick, >=stealth'},
    twopaths/.style={<->, ultra thick, >=stealth'}
]

% Define observed variables
\node [manifest] (BSS01T1) at (0,0) {BSS01 \at T1};
\node [manifest] (BSS02T1) [below=of BSS01T1]  {BSS02 \at T1};
\node [manifest] (BSS03T1) [below=of BSS02T1]  {BSS03 \at T1};

\node [manifest] (BSS17T1) [below=2cm of BSS03T1]  {BSS16 \at T1};
\node [manifest] (BSS18T1) [below=of BSS17T1]  {BSS17 \at T1};
\node [manifest] (BSS19T1) [below=of BSS18T1]  {BSS19 \at T1};

\node [manifest] (BSS01T2) [below=2cm of BSS19T1]  {BSS01 \at T2};
\node [manifest] (BSS02T2) [below=of BSS01T2]  {BSS02 \at T2};
\node [manifest] (BSS03T2) [below=of BSS02T2]  {BSS03 \at T2};

\node [manifest] (BSS17T2) [below=2cm of BSS03T2]  {BSS17 \at  T2};
\node [manifest] (BSS18T2) [below=of BSS17T2]  {BSS18 \at  T2};
\node [manifest] (BSS19T2) [below=of BSS18T2]  {BSS19 \at  T2};


% Draw dashed vertical lines to show that items in middle of questionnaire are not shown
\draw[dashed, ultra thick] (BSS03T1.south) -- (BSS17T1.north);
\draw[dashed, ultra thick] (BSS03T2.south) -- (BSS17T2.north); 


% Define latent variables
\node [latent] (SIT1) [left=3.5cm of BSS17T1] {Suicidal \\ Ideation \at T1};
\node [latent] (SIT2) [left=3.5cm of BSS03T2] {Suicidal \\ Ideation \at T2};


% Draw paths form latent to observed variables
\foreach \all in {BSS01T1, BSS02T1, BSS03T1, BSS17T1, BSS18T1, BSS19T1}{
    \draw [paths] (SIT1.east) to node { } (\all.west);
}

\foreach \all in {BSS01T2, BSS02T2, BSS03T2, BSS17T2, BSS18T2, BSS19T2}{
    \draw [paths] (SIT2.east) to node { } (\all.west);
}


% Draw lines to indicate (residual) covariances
\draw [twopaths] (BSS01T1.east) to [bend left=90] (BSS01T2.east);
\draw [twopaths] (BSS02T1.east) to [bend left=90] (BSS02T2.east);
\draw [twopaths] (BSS03T1.east) to [bend left=90] (BSS03T2.east);
\draw [twopaths] (BSS17T1.east) to [bend left=90] (BSS17T2.east);
\draw [twopaths] (BSS18T1.east) to [bend left=90] (BSS18T2.east);
\draw [twopaths] (BSS19T1.east) to [bend left=90] (BSS19T2.east);
\draw [twopaths] (SIT1.west) to [bend right=90] (SIT2);


\end{tikzpicture}
\end{document}

Thursday, July 12, 2012

Keywords in Latex APA style

According to the APA manual, up to five keywords should be provided on the abstract page. Using the \keywords command with \documentclass[nobf,man]{apa} may not work. Workaround:

\abstract{
Provide the text of the abstract as usual here.
\\
\hspace{10mm} \textit{Keywords}: keyword1, keyword2, keyword3, keyword4, keyword 5
}

Tuesday, September 13, 2011

Long tables in LaTeX using apa.cls

I needed to print a tbale spanning multiple pages in my manuscript, but could not get it to work by passing the longtable option to apa.cls. The table still didn't fit on one page when I used the \small command (see this post). Only the first page of the table would be inserted, and in some cases even the numbering of pages went astray. So I decided to resort to a less clean solution: cutting up the table into parts myself. Actually, this approach is nicely described on page 47 of this document.

At first I got stuck using the \addtocounter{table}{-1} command. If you use itbetween tables, the commands are added up and subtracted from the original couter value. That is, if you use it 5 times, the first table will be numbered -4, the second -3, etc. If you use it directly after the \begin{table} command, it works out fine.


Example

\documentclass[man]{apa}
\begin{document}

\begin{table}
\caption{The First Table in the Manuscript}
\begin{tabular}{ll}
1 & 2 \\
3 & 4 \\
\end{tabular}
\label{tablelabel}
\end{table}

\begin{table}
\addtocounter{table}{-1}
\caption{Continued}
\begin{tabular}{ll}
5 & 6 \\
7 & 8 \\
\end{tabular}
\end{table}

\begin{table}
\addtocounter{table}{-1}
\caption{Continued}
\begin{tabular}{ll}
9 & 10 \\
11 & 12 \\
\end{tabular}
\end{table}

\begin{table}
\caption{The Second Table in the Manuscript}
\begin{tabular}{ll}
a & b \\
c & d \\
\end{tabular}
\end{table}

\end{document}

Thursday, August 4, 2011

LaTeX tables in APA format


Though LaTeX can produce nice-looking tables, the process can be time consuming and frustrating, especially if you want to use APA format rules. Some tips & tricks, and an example are provided below.


Some usefull commands


\hline: creates a horizontal line, which you can use within a table
\thickline: creates a thicker horizontal line, which you can use to separate the caption from the table
\cmidrule{}: can be used to create a midline, spanning only a subset of the columns
\multicolumn{}: can be used to let a cell span multiple columns
\multirow{}: can be used to let a cell span multiple rows
 

\label{}: should be used at the end of the table, I prefer to use it just before \end{table}


Tips and remarks

When using \multirow{}, add extra rows to fit in the text (i.e., if you use a \multirow command spanning three rows, add two extra rows by typing \\ \\, or some text will be left out, or printed over the text in the rows, following the multirow).

Smaller typeface for tables: If you've got a huge table, letting it span multiple pages is difficult (i.e., I can't get it to work with the APA document class). Instead, using \small or \tiny makes the typeface smaller, so maybe it'll make your table fit on one page.


Rotate tables: To rotate the table, you can use the rotating package: type \usepackage{rotating} in the preamble, and use \begin{sidewaystable} and \end{sidewaystable} instead of \begin{table} and \end{table}. This will affect the order of the tables, however, making the sideways tables come first. By declaring \DeclareDelayedFloatFlavor{sidewaystable}{table} somewhere after the start of the document, this will be fixed. Please note that the table will not end up sideways in the DVI file, but it will in postscript or pdf files.


Example

\documentclass[nobf,man,longtable]{apa} % turns it into an APA manuscript

\usepackage{ctable} % needed for \cmidrule{}
\usepackage{multirow} % needed for \multirow{}

\begin{document}
\begin{table}
\caption{Table in APA Format}
\begin{flushleft}
\small
\begin{tabular}{cccc}
\thickline
Column 1 & \multicolumn{3}{c}{Column 2} \\
\cmidrule(lr){2-4}
& Column 2a & Column 2b & Column2c \\
\hline
a & 1 & .67 & 5 \\
b & 2 & .32 & 2 \\
c & 3 & .01 & 4 \\
\hline
\multicolumn{4}{l}{\multirow{2}{90mm}{Note: This text is spanning 4 columns and 2 rows, and it is left justified.}} \\
\\
\end{tabular}
\end{flushleft}
\label{table:APA_small_typeface}
\end{table}

\end{document}