TransWikia.com

How to use input in newcommand?

TeX - LaTeX Asked on January 18, 2021

I want to implement an insert text wrapped in some additional tags using the input command. But I get LaTeX Error: There's no line here to end.. How to do it properly? Here is my minimal reproducible example:

style.cls – defines commands

NeedsTeXFormat{LaTeX2e}
ProvidesClass{style}[Some class]
LoadClass{report}


newcommand{task}[1]{  % Command for defining the text that I want to insert
  gdef@task{#1}
}

newcommand{typeTask}{  % Command to insert text into some kind of wrapper
    begin{center}
        MakeUppercase{task}
    end{center}
    vspace{4mm}
    @task   % Text declared using task command
    vspace{4mm}
    rule{4cm}{0.4pt}
}

main.tex – main tex file

documentclass[a4paper,oneside,12pt]{style}
begin{document}
task{input{to_include}}  % Including text from another tex file and defining as @task
typeTask  % Inserting wrapped defined text
end{document}

to_include.tex – latex text to include

Some text
begin{enumerate}
    item item 1
    item another item
        subitem and subitems
        subitem more subitems
    item latest item
end{enumerate}

I want to get the result corresponding to the LaTeX file:

documentclass[a4paper,oneside,12pt]{report}
begin{document}
    { % Header of wrapper from style.cls
        begin{center}
            MakeUppercase{task}
        end{center}
        vspace{4mm}
    }
    { % From include file
        Some text
        begin{enumerate}
            item item 1
            item another item
                subitem and subitems
                subitem more subitems
            item latest item
        end{enumerate}
    }
    { % Footer of wrapper from style.cls
        vspace{4mm}
        rule{4cm}{0.4pt}
    }
end{document}

instead of error. How to do this properly?

One Answer

The error is produced by if there is no line for it to end, it is not related to input or newcommand really.

You have

    vspace{4mm}
    @task   % Text declared using task command
    vspace{4mm}

so if @task (which is the text supplied in the argument) has already ended the paragraph, the here will be in error.

If @task does not end the paragraph (eg if the argument was Hello World then you would get no error but undesired behaviour as the would force a line break but the following vspace would not add space at that point but after the following line.

Almost always when using vspace you want a blank line (or equivalently, par) before the space to get into vertical mode.

so

    vspace{4mm}
    @task par  % Text declared using task command
    vspace{4mm}

Correct answer by David Carlisle on January 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP