Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How do I set a \newcommand so that the field #1, which is calculated to be 1.2*#2 Optional? Something similar to the following commands, but that did not work.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\newlength{\myskip}
\setlength{\myskip}{1.2}

\newcommand{\mysize}[3][#2\myskip]{{\fontsize{#2}{#1}\selectfont #3}}
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

If you want the first parameter to be optional you need to define what the default value is:

\newcommand{\mysize}[3][12pt]{\fontsize{#2}{#1}\selectfont #3}

So, now #1 will have the value of 12pt if it is not specified.


If you want to #1 to depend on #2, you can use the xparse package:

\DeclareDocumentCommand{\mysize}{o m m}{%
    \setlength{\myskip}{#2}%
    \setlength{\myskip}{1.2\myskip}%
    \IfNoValueTF{#1}{\fontsize{#2}{\myskip}}{\fontsize{#2}{#1}}%
    \selectfont%
    \textbf{#3}%
}

\IfNoValueTF is used to test if #1 was specified, and the o m m specify that the first parameter is optional and that the next two are mandatory. Using this with \mysize{10pt}{Default First Parameter:} and \mysize[16pt]{10pt}{First Parameter Specified:} yields:

enter image description here

\documentclass{article}
\usepackage{lipsum}
\usepackage{xparse}

\newlength{\myskip}
\DeclareDocumentCommand{\mysize}{o m m}{%
    \setlength{\myskip}{#2}%
    \setlength{\myskip}{1.2\myskip}%
    \IfNoValueTF{#1}{\fontsize{#2}{\myskip}}{\fontsize{#2}{#1}}%
    \selectfont%
    \textbf{#3}%
}

\newcommand{\Text}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed erat ante, elementum sed dignissim non, faucibus id eros. Nam gravida lacus non diam rhoncus quis facilisis augue congue. Mauris a sem arcu, eget elementum arcu. Curabitur pellentesque ultrices mauris, placerat volutpat nulla vulputate sed.}%

\begin{document}
\mysize{10pt}{Default First Parameter:}
\Text

\mysize[16pt]{10pt}{First Parameter Specified:}
\Text
\end{document}
share|improve this answer
    
Thank you all!! –  Regis da Silva Dec 29 '11 at 17:59
add comment

Here is an option that might work:

enter image description here

\documentclass{article}

\newcommand{\Text}{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris.}

\makeatletter
\newcommand{\mysize}[3][1.2]{%
  \set@fontsize\baselinestretch{#2}{#2}%
  \set@fontsize{#1}\f@size\f@baselineskip%
  {\selectfont#3}
}
\makeatother
\begin{document}
\Text \par
\texttt{baselineskip}: \the\baselineskip \par

\medskip

\mysize{14}{\Text \par
\texttt{baselineskip}: \the\baselineskip} \par

\medskip

\mysize{14pt}{\Text \par
\texttt{baselineskip}: \the\baselineskip} \par

\medskip

\mysize[1.5]{14pt}{\Text \par
\texttt{baselineskip}: \the\baselineskip}
\end{document}

The above example provides \mysize[<factor>]{<size>}{<stuff>} that sets the font size for <stuff> to <size> with a \baselinestretch of <factor> (default is 1.2). This solution is based on the definitions of \fontsize and \linespread in latex.ltx:

\DeclareRobustCommand\linespread[1]
  {\set@fontsize{#1}\f@size\f@baselineskip}
\DeclareRobustCommand\fontsize[2]
  {\set@fontsize\baselinestretch{#1}{#2}}

The advantage of this approach is that you can specify your font sizes by numerals or point size: \mysize{12}{...} is the same as \mysize{12pt}{...}. The disadvantage of this approach in general is that only the font size and \baselineskip is modified, and not \parskip. And, since paragraphs are allowed in \mysize by default (since we used \newcommand), line spacing between paragraphs will not look that great; this is already visible in the above example between \Text and the \baselineskip length print-out. Modification of the line spacing should in principle be handled by setspace.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.