How to Indent text in LaTeX in a custom way - latex

Hi I'm new to LaTeX and I'm having some troubles in write a Use Case Specification using the style in this picture.I've tried using listings but i can't succeed in underlying some words.Anyone can help me?

I made a command so you can easily without much typing make multiple usecases, this is the complete latex code with 1 example, you can modify the command, it takes 5 parameters and you can access them with the #:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{enumitem}
\newcommand{\spec}[5]{
\begin{description}
\item[\underline{SpecificaUseCase}] #1 \\
#2
\begin{itemize}[noitemsep,topsep=-8pt]
\item[] \underline{pre}: #3
\item[] \underline{post}: #4
\end{itemize}
\item[\underline{FinalSecifica}] #5 \\
\end{description}
}
\begin{document}
\spec{usecasename}{method}{pre stuff}{post stuff}{final}
\end{document}
The result of this code is somthing like this:

Related

Real operations in LaTex

This is a piece of my code.
\newcommand{\number}{2}
\number + 2
Is there any way that a '4' is written instead a '2+2'? (LaTex does the real operation).
Thanks
There are many possibilities to do calculations in latex. Just one option is pgfmath:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\newcommand{\mynumber}{2}
\pgfmathparse{\mynumber+2}
\pgfmathresult
\end{document}

\sbox from inside an environment

I'm trying to save some text inside an environment for later use. The smallest test case I could come up with is this. The saved text in the sbox isn't available after the environment is closed. How can I work around that? Thanks.
\documentclass{article}
\begin{document}
\newsavebox{\somebox}
\begin{itemize}
\item hello1
\item hello1 \sbox{\somebox}{Some text}
\end{itemize}
This should show something, but does not: "\usebox{\somebox}"
\end{document}
What you're running into here is a scoping issue. In (La)TeX, you can introduce scopes with { ... }, \bgroup ... \egroup, or \begingroup ... \endgroup. The former two are roughly the same, as \bgroup and \egroup are defined by \let\bgroup{ and \let\egroup}; the last one is slightly different. But the scoping property is the same: any commands, boxen, etc., created or modified within those scopes are not visible outside. And in LaTeX, all environments \begin{env} ... \end{env} implicitly wrap their contents in \begingroup ... \endgroup. This means that your\sbox{\somebox}{Some text} modification is only visible until the \end{itemize}; after that, the modification is undone. To get around this, prepend any command like \newcommand, \def, \newsavebox, \sbox, etc., with \global, which forces the definition to take place at the global scope and be visible everywhere.
Also, to use quotes in (La)TeX, write ``double quoted'', ``double quoted", or `single quoted'; the " character is only for closing quotes, not opening quotes. Putting this all together gives you the revised snippet
\documentclass{article}
\begin{document}
\newsavebox{\somebox}
\begin{itemize}
\item hello1
\item hello1 \global\sbox{\somebox}{Some text}
\end{itemize}
This should show something, and in fact does: ``\usebox{\somebox}''
\end{document}
I think I can work around this by using \def. like so:
\documentclass{article}
\begin{document}
\begin{itemize}
\item hello1
\item hello1
\global \def \somebox {Some text}
\end{itemize}
This should show something: \somebox
\end{document}

How to get the value of the document title in latex?

I am wondering how I can get the document title in LaTex, for use elsewhere in the document. I just want to be able to echo it.
Using \#title does not work because \maketitle clears \#title. This seems silly to me but that's the way it is. One solution is to redefine \title to save the title somewhere else. For instance,
\def\title#1{\gdef\#title{#1}\gdef\THETITLE{#1}}
then use \THETITLE.
You can do the other way around: \def\MYTITLE{...} then \title{\MYTITLE} and later use \MYTITLE again.
I had success just writing a new command.
\newcommand{\mytitle}{...}
\title{\mytitle}
There is a package called authoraftertitle that does exactly this
\documentclass{article}
\usepackage{authoraftertitle}
\setlength\parindent{0 pt}
\begin{document}
\title{a good title}
\author{a better author}
\date{the best date}
\maketitle
the title is: \textbf{\MyTitle} \\
the author is: \textbf{\MyAuthor} \\
the data is: \textbf{\MyDate} \\
\end{document}
This is a workaround...
\let\titleoriginal\title % save original \title macro
\renewcommand{\title}[1]{ % substitute for a new \title
\titleoriginal{#1}% % define the real title
\newcommand{\thetitle}{#1} % define \thetitle
}
\title{This is my title}
\begin{document}
\thetitle
\end{document}
The short version of the title was ignored here...

New environment in latex using other environments, compiler doesn't find the \end

I'm setting up a new environment for my latex document for consistent tables. It looks like this:
\newenvironment{defaultTable}[2] {
\begin{table}[h]
\noindent
\tabularx{\textwidth}{#1}
\specialrule{0.5pt}{10pt}{0pt} \rowcolor[gray]{.9}
} {
\bottomrule
\endtabularx
\caption{#2}
\end{table}
}
It doesn't seem to find the \end{table} though:
! LaTeX Error: \begin{table} on input line 23 ended by \end{document}.
Is there a way to avoid this?
Replace \begin{table} with \#float{table} and replace \end{table} with \end#float.
The \#float and \end#float are LaTeX's internal commands for starting and ending the float environment.
You'll also want to follow Alexey's advice on the #2 parameter. Store it in the first part of your environment (\gdef\mycaption{#2}) and then recall it later \caption{\mycaption} in the second part. Put \def\mycaption{\relax} just before the \begin{defaultTable} line.
Also, since \#float and \end#float have # signs in them, if this code is in the preamble of your document file (instead of say, a .sty file), you'll need to put \makeatletter before your \begin{defaultTable} and also \makeatother after \end{defaultTable}.
You can use #2 in the end if you use the xparse mechanism:
\usepackage{xparse}
\NewDocumentEnvironment{defaultTable}{+m+m}{%
\begin{table}[h]
\noindent
\tabularx{\textwidth}{#1}
\specialrule{0.5pt}{10pt}{0pt} \rowcolor[gray]{.9}
} {%
\bottomrule
\endtabularx
\caption{#2}
\end{table}
}
You can not use #2 in the last argument of the \newenvironment macros. You should use #1..#9 in the second argument only.
Save your #2 to \tempa (or any macros). And use \tempa in the caption.
\newenvironment{defaultTable}[2]{
\begin{table}[h]
\def\tempa{#2}
\noindent
\tabularx{\textwidth}{#1} \specialrule{0.5pt}{10pt}{0pt} \rowcolor[gray]{.9}
}{
\bottomrule
\endtabularx
\caption{\tempa}
\end{table}
}
I've has the same problem, and it is because of the "\end{tabularx}". The solution is:
\newenvironment{defaultTable}[3] {
\begin{table}[h]
\caption{#2}
\noindent
\begin{tabularx}{\textwidth}{#1}
\specialrule{0.5pt}{10pt}{0pt} \rowcolor[gray]{.9}
#3
\bottomrule
\end{tabularx} } {
\end{table} }
So you define the rows as a parameter.
Regards,
Eric
You could also just use a \newcommand similar to Eric's solution.
\documentclass{article}
\usepackage{tabularx}
% The table design.
\newcommand{\defaultTable}[2]{
\begin{table}[h]
\begin{tabularx}{\textwidth}{cc}
Column A & Column B \\
#2
\end{tabularx}
\caption{#1}
\end{table}
}
\newcommand{\defaultTableRow}[2]{#1 & #2 \\}
\begin{document}
% The creation of a table.
\defaultTable{Example}{
\defaultTableRow{bla}{0815}
\defaultTableRow{blup}{0815}
}
\end{document}
This will avoid both your problems (the missing \end{table} and the error when referencing arguments in the environments closing code) without much hassle.
In fact I also like the idea of separating the table design from the table data. Especially if you create multiple tables that need to look equal.

How to make a signature field in LaTeX

When I'm writing my laboratory reports in LaTeX, I recently had to make a signature field on the form.
Trondheim 4.september 2009
______________________ ___________________________
Ivar Nesje Team mate's name
The problem was that I could not find a easy way to do it so after a lot of searching on the net I came up with this simple solution
\newcommand{\doubleSignature}[3]{
\begin{minipage}[c]{\textwidth}
\vspace{2cm}
\makebox[12cm][c]{
#1, \today
}
\vspace{3cm}
\makebox[12cm][c]{
\hfill \makebox[5cm][c] {\hrulefill} \hfill \makebox[5cm][c] {\hrulefill} \hfill
}
\makebox[12cm][c]{
\hfill #2 \hfill #3 \hfill
}
\vspace{1cm}
\end{minipage}
}
This allowed me to type;
\doubleSignature{Trondheim}{Ivar Nesje}{Team mate's name}
To achieve the wanted result. If any of you have another way of doing this, not using the letter document class, I'd be really glad to hear your suggestions.
I think I would work from \rule[<raise>]{<width>}{<thickness>}. Because the reference point is the lower left corner, you probably don't even need the optional raising argument. Something like:
\newcommand{\doublesignature}[3][Ivar Nesje]{%
\parbox{\textwidth}{
\centering #3 \today\\
\vspace{2cm}
\parbox{7cm}{
\centering
\rule{6cm}{1pt}\\
#1
}
\hfill
\parbox{7cm}{
\centering
\rule{6cm}{1pt}\\
#2
}
}
}
I've re-ordered your arguments to make your name optional.
Assuming there aren't additional formatting restrictions, I probably would have used a tabular environment instead of boxes, and I would have used \rule{length}{width} instead of \hrulefill:
\newcommand{\doubleSignature}[3]{
\vspace{2cm}
\begin{center}
#1, \today
\end{center}
\vspace{3cm}
\noindent
\begin{tabular}{lcl}
\rule{5cm}{1pt} & \hspace{2cm} & \rule{5cm}{1pt} \\
#2 & & #3
\end{tabular}
\vspace{1cm}
}

Resources