newcommand containing loop causes an error in a tabular - tabular

I have the little program below where some cells of a tabular are built by a newcommand. If the newcommand content is put directly in the tabular, all is ok. If I embed a new command, I get "Missing number, treated as 0" at the calling line.
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\title{test}
\author{myself}
\date{\today}
\begin{document}
\maketitle
\def\tand{&}
\newcounter{i}
\newcommand{\doit}{
\makeatletter
\setcounter{i}{3}
\#whilenum\value{i}>0\do{}
{
P\value{i} \tand%
\addtocounter{i}{-1}
} %
\makeatother
P0 %
}
\newcounter{it}
\begin{tabular}{|c|c|c|c|}
3 & 2 & 1 & 0 \\
\doit \\
\end{tabular}
\end{document}
Help would be welcome.

Related

How to center this piece of text in ShareLateX (editor) or Latex (in general)?

I am writing a resume and want to center text at the top of the document which contains my name and email details, like this
I used \begin{center} and \end{center} but it doesnt seem to work
Here is my code:
\begin{document}
\begin{tabular*}{\linewidth}{l#{\extracolsep{\fill}}r}
\vspace{-4mm}
\textbf{\LARGE Samuel Adams}
\\
& \\ \textbf{Notsville, TX} $|$
\textbf{\phone \ \ +1 (000) 000 0000} $|$
\textbf{cxcx#gmail.com} $|$ \textbf{{www.linkedin.com/ffdfs}}\\
\end{tabular*}
\vspace{1 mm}
Can anyone help me center this piece of text?
This is how it looks right now. I am looking to center both lines just like we do it in word.
The table will be in the middle of the page once
you make the left and right margin the same size
use \noindent in front of the table to avoid it being indented as the first line of a new paragraph
(I'm actually not sure why you use a table for this, you never use the second column...)
\documentclass[a4paper,10pt]{article}
\usepackage[empty]{fullpage}
\usepackage{xcolor}
\usepackage{url}
\usepackage{wasysym}
\usepackage{hyperref}
\usepackage{enumitem}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,bindingoffset=0.3in, left=0.5in,right=0.5in,top =0.8in,bottom=-5in, footskip=.25in]{geometry}
\begin{document}
\noindent\begin{tabular*}{\linewidth}{l#{\extracolsep{\fill}}r}
\textbf{\LARGE Samuel Adams} & \\
\textbf{Notsville, TX} $|$ \textbf{\phone \ \ +1 (000) 000 0000} $|$
\textbf{cxcx#gmail.com} $|$ \textbf{{www.linkedin.com/ffdfs}} & \\
\hline
\end{tabular*}
\vspace{1 mm}
\end{document}

Error in using \cite and \bibliography in Overleaf

Today Overleaf behaving weird and I am not getting what's wrong in citation and references. I tried a very simple one line code, but getting error.
Please let me know what's wrong in my .tex and .bib file
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\begin{document}
Einsteins stated the \cite{einstein}
\bibliography{sample.bib}
\end{document}
And here is my sample.bib
#article{einstein,
author = "Albert Einstein",
title = "{Zur Elektrodynamik bewegter K{\"o}rper}. ({German})
[{On} the electrodynamics of moving bodies]",
journal = "Annalen der Physik",
volume = "322",
number = "10",
pages = "891--921",
year = "1905",
DOI = "http://dx.doi.org/10.1002/andp.19053221004",
keywords = "physics"
}
Two problems:
you must set a bib style with \bibliographystyle{...}
don't add a file type in \bibliography{...}
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\begin{document}
Einsteins stated the \cite{einstein}
\bibliographystyle{plain}
\bibliography{sample}
\end{document}

How to refer a table from one chapter in another chapter in a report

I am writing a report, my document class is report, and I need to refer a table defined in one chapter in another chapter. I am using \usepackage{hyperref} too, however I am unaware of the code that has to be used in the text. Please help me to find a solution for this issue.
You should use the \label and \ref commands:
\label assigns a label (nomen omen) and can be given to an equation, a table, a figure.
\ref is used to refer to a particular label.
For example, if you have a figure and assign to it the label fig1 by using the command \label{fig1} then in the text you can refer to that figure by writing, for example
As it is clear in \ref{fig1}, the colors are bright and vivid.
Take a look here for some guidance. Down below you can find an example related to your question.
\documentclass{report}
\usepackage{lipsum}
\begin{document}
\chapter{First Chapter}
\lipsum[1]
\begin{table}[htbp]
\begin{center}
\begin{tabular}{c|c|c}
A & B & C\\
\hline
1 & 2 & 3\\
4 & 5 & 6
\end{tabular}
\end{center}
\caption{A table}\label{tab}
\end{table}
\lipsum[1-3]
\chapter{Second chapter}
In this chapter we are referring to Table \ref{tab}. In the following we analyse the data and compare them to Table \ref{tab2}.
\begin{table}[htbp]
\begin{center}
\begin{tabular}{c|c|c}
D & E & F\\
\hline
-1 & -2 & -3\\
-4 & -5 & -6
\end{tabular}
\end{center}
\caption{Another table}\label{tab2}
\end{table}
\end{document}
You may use also the package cleveref to let Latex handle the correct names of labelled environments (equations, tables, figures...).
You can use latex's \label/\ref mechanism like in the following example:
\documentclass{report}
\usepackage{hyperref}
\begin{document}
\chapter{chapter 1}
\begin{table}[htbp]
...
\caption{some table}
\label{mytable}
\end{table}
\chapter{chapter 2}
see my table \ref{mytable}
\end{document}

Directly use gnuplot from octave?

I generally find Octave to be the easiest-to-use tool for data evaluation, but cumbersome for data-visualization.
On the other hand I find Gnuplot to be the easiest-to-use tool for data-visualization because of
its very focused nature,
its strong library of demo examples, and
its comprehensive interactive help features.
These advantages are sabotaged by putting any language-wrapper between gnuplot and the user, but pure gnuplot (intentionally) lacks capabilities for doing any non-trivial preprocessing (e.g. numerical calculus).
Octave can use gnuplot as a backend, but in order to preserve the advantages I would prefer directly using gnuplot commands, say:
dx = 0.1;
x = 0:dx:2*pi;
y = sin(x);
y2 = cumsum(y)*dx;
outputFileName = "sin.pdf"
# Contains mockup syntax:
gp set term pdfcairo size 30cm,20cm noenhanced
gp set output '${outputFileName}'
gp plot ${x,y} using 1:2 with lines title "The sin(x) function", \
gp ${x,y2} using 1:2 with lines title "Integrated sin(x) function", \
gp cos(x) title "The cos(x) function"
gprun
Writing a basic implementation of this "gp" utility would be easy enough, though variable interpolation would have to be replaced by explicit gp(["set output '" outputFileName "'"]) at the cost of readability. Not so easy however would be the creation of the ${x,y} table file.
Is there any straight-forward way to obtain similar functionality?
Ironically, some much older versions of octave (up until around 2005, 2006) had this capability (gset, graw, ...) but had them demoted to an internal implementation detail. In some older discussions I find references to e.g. __graw__, which also doesn't exist anymore, and some outdated documentation sites mention gplot, which still exists but with an entirely different purpose.
Honestly, I think you're overthinking it. Creating a temporary script and running it is often even more straightforward and portable than cumbersome libraries (e.g. see my previous similar answer here for an sqlite example )
Using that approach below works well for me, is very readable in my opinion, and does not result in unnecessary files (at least not visibly on your workspace).
dx = 0.1;
x = 0 : dx : 2 * pi;
y = sin( x );
y2 = cumsum( y ) * dx;
outputFileName = "sin.pdf"
% Create Gnuplot Script
GPScriptName = tempname(); gp = fopen( GPScriptName, 'w');
fdisp( gp, 'set term pdfcairo size 30cm,20cm noenhanced' );
fdisp( gp, sprintf( 'set output "%s"', outputFileName ) );
fdisp( gp, 'plot "-" using 1:2 with lines title "The sin(x) function", \' );
fdisp( gp, ' "-" using 1:2 with lines title "Integrated sin(x) function", \' );
fdisp( gp, ' cos(x) title "The cos(x) function"' );
fdisp( gp, [ x(:), y(:) ] );
fdisp( gp, 'e' );
fdisp( gp, [ x(:), y2(:) ] );
fdisp( gp, 'e' );
fdisp( gp, ';' );
fclose( gp )
% Run script with Gnuplot
Cmd = sprintf( 'gnuplot --persist "%s"', GPScriptName );
[Status, Output] = system( Cmd );
I used 'inline' data here, so that I could dump it straight in the script (as suggested here), but if you prefer, you could use the same logic to fdisp them to a separate temporary Data file, and make your Gnuplot script read from that; also you may or may not handle status errors and delete the temp files after the operation (like I did in the linked sqlite example) if you're worried about producing lots of temporary files and taking up space ... but in practice it typically doesn't matter.
I think this approach is fine and gives you more control ... but, if you insist, it should be trivial to wrap those fdisp calls into a gp function, and the system call into a gprun function ...

Set plot title inside loop

I am using a gnuplot plot loop to plot data from several plots together:
filenames = "my data files"
plot for file in filenames file.".txt" \
title file
Right now I'm using title file to set the plot title, but I'd like more control over the plot title without resorting to changing my file names. For example, in pseduocode, I'd like:
files = [first, second, third, fourth]
titles = [One title, second title, third title, fourth title]
plot for [n=1:4] files[n] titles[n]
Note that the titles consist of multiple words, so words(titles,n) is not an option.
Is there another method I can use to give me more flexibility in my titles?
First of all: good news, the 5.0 version has limited support for quoting text parts for use with word and words.
With version 5.0RC3, the following works fine:
titles='"first title text" "second title text"'
plot x title word(titles, 1), 2*x title word(titles, 2)
A second 'hack' would work with the postscript terminal, in case you are using it, and encodes the space inside the title with its octal representation \040:
set terminal postscript eps enhanced
set output 'spaces.eps'
titles='first\040title\040text second\040title\040text'
plot x title word(titles, 1), 2*x title word(titles, 2)
A third version uses a replacement character for the spaces and a shell call to sed to insert the spaces after the splitting:
titles='first#title#text second#title#text'
sub(s) = system(sprintf("echo \"%s\" | sed 's/#/ /g'", s))
plot x title sub(word(titles, 1)), 2*x title sub(word(titles, 2))
You could also setup a function myword, which uses awk or similar to do the splitting directly. But that would probably require some fiddling with quote characters.
This is indeed possible using word(string_with_words, index) :
filenames = "my data files"
description= "one two three"
plot for [n=1:4] word(filenames, i) title word(description, i)

Resources