0

I'm trying to set a value to be used inside \vspace{} based on a value provided by a function which reads it from the keyboard. For example, if the provided value is 3, I would like for \vspace{} to be

\vspace{(3-1)0.6 cm}

What I've tried to do, without success, is

\documentclass[]{paper}
\usepackage{calc}

\newcommand{\titlelines}[1]{\def\@TitleLines{#1}}
\titlelines{3}
\newlength{\mylength}
\setlength{\mylength}{{\@TitleLines-1}*0.6}


\title{text \\ \vspace*{\mylength cm} more text}

\begin{document}
\maketitle
\end{document}

I don't know what I'm doing wrong. Can you help me fix this code? Thank you very much.

2

There are a few problems with your code, one of which is invisible. Because you did not use \makeatletter, your definition of \titlelines makes \@ into a macro that eats TitleLines and spits out some number. Because you always follow \@ by TitleLines, this doesn't cause an error.

You should parenthesize subexpressions for calc using parentheses, not braces. Also all dimensions need a unit. Also calc requires decimal numbers to be wrapped in the \real command:

\documentclass[]{paper}
\usepackage{calc}

\makeatletter
\newcommand{\titlelines}[1]{\def\@TitleLines{#1}}
\titlelines{3}
\newlength{\mylength}
\setlength{\mylength}{(\@TitleLines cm-1cm)*\real{0.6}}
\makeatother


\title{text \\ \vspace*{\mylength} more text}

\begin{document}
\maketitle
\end{document}
1

First, because you have an @sign in your macro \@TitleLines, you must put the code that uses this macro between \makeatletter and \makeatother (unless it is in a class or package file). I would, however, recommend to use a macro without @.

Second, the calc package allows multiplication of a length only after the length: \setlength{\mylength}{0.6cm * (\@TitleLines-1)}.

And, third, round parentheses must be used in the expression.

So the code becomes:

\documentclass[]{paper}
\usepackage{calc}
\makeatletter
\newcommand{\titlelines}[1]{\def\@TitleLines{#1}}
\titlelines{3}
\newlength{\mylength}
\setlength{\mylength}{0.6cm * (\@TitleLines-1)}
\makeatother

\title{text \\ \vspace*{\mylength} more text}

\begin{document}
\maketitle
\end{document}
0

I didn't try to fix your attempt, but attempted to answer the implicit question another way. This works by defining the length you want on the compile command line. No makeatletter needed. It may or may not match your workflow.

If the input file is cmddef.tex then compile with

pdflatex "\def\mylength{3 cm} \input{cmddef.tex}"

cmddef.tex:

\documentclass{paper}
%\newcommand{\mylength}{3 cm}

\title{text \\ \vspace*{\mylength{}} more text}

\begin{document}
\maketitle

The value of mylength is \mylength{}.
\end{document}

(Found at Passing parameters to a document)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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