Difference between revisions of "Document Titles"

From Wiki
Jump to navigation Jump to search
m (Another minor wording change.)
m (Clarification of which solution can use headers and footers)
(10 intermediate revisions by 8 users not shown)
Line 1: Line 1:
< [[From LaTeX to ConTeXt]] >
+
< [[From LaTeX to ConTeXt]] | [[Article Abstracts]] >
  
 
== In LaTeX ==
 
== In LaTeX ==
Line 15: Line 15:
 
</texcode>
 
</texcode>
  
The title, author, and date defined by these commands is also used to create the PDF authoring information, if appropriate packages are used. (I'm not sure of the details of this; will look them up later. --Brooks)
+
The title, author, and date defined by these commands is also used to create the PDF authoring information, if appropriate packages, like hyperref, are used. (Hyperref can set these fields using \pdfauthor{\theauthor} etc.)
  
 
== In ConTeXt: A simple solution ==
 
== In ConTeXt: A simple solution ==
Line 39: Line 39:
  
 
<context>
 
<context>
 +
\setuplayout[scale=.7]
 
\starttext
 
\starttext
 
\startalignment[center]
 
\startalignment[center]
   \blank[2*big]
+
   \blank[force,2*big]
 
     {\tfd How to do this in Context}
 
     {\tfd How to do this in Context}
 
   \blank[3*medium]
 
   \blank[3*medium]
Line 57: Line 58:
 
== In ConTeXt: A more reusable solution ==
 
== In ConTeXt: A more reusable solution ==
  
The simple solution replicates the appearance of the LaTeX original, but it doesn't replicate the spirit of it, which is to separate the formatting from the content.  We can do this by creating the following definitions, and putting them in a module file.  The <code>\title</code> and similar commands are copied nearly directly from the LaTeX kernel, and the formatting command the same as the previous version.
+
The simple solution replicates the appearance of the LaTeX original, but it doesn't replicate the spirit of it, which is to separate the formatting from the content.  We can do this by creating the following definitions, and putting them in a module file.  The <code>\title</code> and similar commands are copied nearly directly from the LaTeX kernel, and the formatting command the same as the previous version -- note that we use <code>\doctitle</code> instead of <code>\title</code>, to avoid obliterating the <code>\title</code> command that ConTeXt is already using for unnumbered sections.
  
 
<texcode>
 
<texcode>
 
\unprotect
 
\unprotect
\def\title#1{\gdef\@title{#1}}
+
\def\doctitle#1{\gdef\@title{#1}}
 
\def\author#1{\gdef\@author{#1}}
 
\def\author#1{\gdef\@author{#1}}
 
\def\date#1{\gdef\@date{#1}}
 
\def\date#1{\gdef\@date{#1}}
\date{\today}  % Default to today unless specified otherwise.
+
\date{\currentdate}  % Default to today unless specified otherwise.
  
 
\def\maketitle{%
 
\def\maketitle{%
 
   \startalignment[center]
 
   \startalignment[center]
     \blank[2*big]
+
     \blank[force,2*big]
 
       {\tfd \@title}
 
       {\tfd \@title}
 
     \blank[3*medium]
 
     \blank[3*medium]
Line 79: Line 80:
 
</texcode>
 
</texcode>
  
Suppose that we save this in the file <code>t-title.tex</code>.  Then, we can use this in our main ConTeXt document with the following syntax, which is quite similar to the LaTeX version aside from the addition of the <cmd>usepackage</cmd><tt>[title]</tt> command in place of <code>\documentclass{article}</code>.
+
Suppose that we save this in the file <code>t-title.tex</code>.  Then, we can use this in our main ConTeXt document with the following syntax, which is quite similar to the LaTeX version aside from the addition of the <cmd>usemodule</cmd><tt>[title]</tt> command in place of <code>\documentclass{article}</code>.
  
 
<texcode>
 
<texcode>
\usepackage[title]
+
\usemodule[title]
   \title{How to do this in Context}
+
   \doctitle{How to do this in Context}
 
   \author{The author}
 
   \author{The author}
 
   \date{July 26, 2005}
 
   \date{July 26, 2005}
Line 94: Line 95:
 
</texcode>
 
</texcode>
  
The result is, of course, identical to the previous version.  Now, the <code>t-title.tex</code> macros can be shared between any number of documents, and changes will affect all of the documents that use this file.  
+
The typeset result is, of course, identical to the previous version.  Now, the <code>t-title.tex</code> macros can be shared between any number of documents,  
 +
and changes will affect all of the documents that use this file.  
 +
 
 +
== In ConTeXt: A more advanced solution ==
 +
 
 +
The next solution use an interface inspired by the ones from the core commands and tries to go away from hard coded values.
 +
 
 +
The values for the title are set with the <code>\setuptitle</code> command, it provides two arguments where the second argument is optional and could be used to change the layout for the three text blocks. The title block itself is placed with the <code>\placetitle</code> command.
 +
 
 +
<texcode>
 +
\unprotect
 +
 
 +
\definesystemvariable {dt} % DocumentTitle
 +
 
 +
% Expand both 1- and 2- arity'd versions of /dosetuptitle
 +
\def\setuptitle
 +
  {\dodoubleempty\dosetuptitle}
 +
 
 +
% The one-argument iteration:
 +
% For each comma separated item of arg1, define (dtcontent(arg1))...
 +
% using \setuptitle in your document as defined at the bottom of this file
 +
 
 +
% The two-argument iteration:
 +
% For each comma separated item of arg1, and using style/color in arg2...
 +
% define (dt(arg1)(style)) and (dt(arg1)(color))
 +
 
 +
\def\dosetuptitle[#1][#2]%
 +
  {\ifsecondargument
 +
    \dodosetuptitle[#1][#2]%
 +
  \else
 +
    \dodosetuptitle[\v!content][#1]%
 +
  \fi}
 +
 
 +
\def\dodosetuptitle[#1][#2]%
 +
  {\def\dododosetuptitle##1%
 +
    {\getparameters[\??dt##1][#2]}%
 +
  \processcommalist[#1]\dododosetuptitle}
 +
 
 +
% Using earlier style/color definitions, apply style and color to arg1-item values
 +
\def\placetitle
 +
  {\startalignment[\v!middle]
 +
  \blank[\v!force,2*\v!big]
 +
  \doattributes{\??dt\c!title}\c!style\c!color\@@dtcontenttitle
 +
  \blank[3*\v!medium]
 +
  \doattributes{\??dt\c!author}\c!style\c!color\@@dtcontentauthor
 +
  \blank[2*\v!medium]
 +
  \doattributes{\??dt\c!date}\c!style\c!color\@@dtcontentdate
 +
  \blank[3*\v!medium]
 +
  \stopalignment}
 +
 
 +
 
 +
% Here is where we define the key-value pairs
 +
\setuptitle
 +
  [\c!title]
 +
  [\c!style=\tfd,
 +
  \c!color=]
 +
 
 +
\setuptitle
 +
  [\c!author,\c!date]
 +
  [\c!style=\tfa,
 +
  \c!color=]
 +
 
 +
\setuptitle
 +
  [\c!title=,
 +
  \c!author=,
 +
  \c!date=\currentdate]
 +
 
 +
\protect
 +
</texcode>
 +
 
 +
To reuse the definition above you could save the code in module like <code>t-title.tex</code> and load it in your document with <code>\usemodule[title]</code>.
 +
 
 +
<texcode>
 +
\setuptitle
 +
  [title={How to do this in Context},
 +
  author={The author},
 +
  date={July 26, 2005}]
 +
 
 +
\starttext
 +
 
 +
\placetitle
 +
 
 +
Then, the actual text of the document starts here.  We'll put in enough text to
 +
fill out the line and start to make a paragraph.
 +
 
 +
\stoptext
 +
</texcode>
 +
 
 +
The definition for the macro <code>\placetitle</code> could be extended without problems to change the values for the space before and after the title block or the distance between all elements with the <code>\setuptitle</code> command.
 +
 
 +
== Adding markings to headers and footers (in "A more reusable solution") ==
 +
 
 +
With just a few extra lines and two redefined definitions in this module,
 +
like so:
 +
 
 +
<texcode>
 +
\unprotect
 +
\definemarking[Author]
 +
\definemarking[Doctitle]
 +
 
 +
\def\doctitle#1{\gdef\@title{#1}\marking[Doctitle]{#1}}
 +
\def\author#1{\gdef\@author{#1}\marking[Author]{#1}}
 +
 
 +
% rest of the file as above
 +
</texcode>
 +
 
 +
allow you to use the document title and author in your headers and
 +
footers:
 +
 
 +
<texcode>
 +
\usemodule[title]
 +
 
 +
\doctitle{How to do this in Context}
 +
\author{The author}
 +
\date{July 26, 2005}
 +
 
 +
\setupheadertexts[{\getmarking[Doctitle]}][{\getmarking[Author]}]
 +
 
 +
\starttext
 +
  \maketitle
 +
  Then, the actual text of the document starts here.  We'll put in enough text to
 +
  fill out the line and start to make a paragraph.
 +
\stoptext
 +
</texcode>
 +
 
  
 
<context>
 
<context>
 +
\setuplayout[scale=.7]
 
\unprotect
 
\unprotect
\def\title#1{\gdef\@title{#1}}
+
\definemarking[Author]
\def\author#1{\gdef\@author{#1}}
+
\definemarking[Doctitle]
 +
 
 +
\def\doctitle#1{\gdef\@title{#1}\marking[Doctitle]{#1}}
 +
\def\author#1{\gdef\@author{#1}\marking[Author]{#1}}
 
\def\date#1{\gdef\@date{#1}}
 
\def\date#1{\gdef\@date{#1}}
\date{\today}  % Default to today unless specified otherwise.
+
\date{\currentdate}  % Default to today unless specified otherwise.
  
 
\def\maketitle{%
 
\def\maketitle{%
Line 115: Line 244:
 
\protect
 
\protect
  
\title{How to do this in Context}
+
\doctitle{How to do this in Context}
 
\author{The author}
 
\author{The author}
 
\date{July 26, 2005}
 
\date{July 26, 2005}
 +
 +
\setupheadertexts[{\getmarking[Doctitle]}][{\getmarking[Author]}]
  
 
\starttext
 
\starttext
Line 125: Line 256:
 
\stoptext
 
\stoptext
 
</context>
 
</context>
 +
 +
 +
So, that's the title.  The abstract can be handled very similarly; see [[Article Abstracts]] for details.
 +
 +
[[Category:From LaTeX]]

Revision as of 22:54, 19 January 2017

< From LaTeX to ConTeXt | Article Abstracts >

In LaTeX

The standard LaTeX document classes define a \maketitle command, along with \title, \author, and \date commands, which can be used to produce a relatively automatic title block.

\documentclass{article}
  \title{How to do this in Context}
  \author{The author}
  \date{July 26, 2005}
\begin{document}
  \maketitle
\end{document}

The title, author, and date defined by these commands is also used to create the PDF authoring information, if appropriate packages, like hyperref, are used. (Hyperref can set these fields using \pdfauthor{\theauthor} etc.)

In ConTeXt: A simple solution

The ConTeXt philosophy is for the author to specify the formatting, rather than selecting from existing formats. A simple solution is thus to write the title block directly; the following version is a close approximation of the version produced by the \maketitle command in LaTeX's startard article class. Obviously, the fine details of spacing and font sizes and so forth can be adjusted as desired.

\starttext
\startalignment[center]
  \blank[2*big]
    {\tfd How to do this in Context}
  \blank[3*medium]
    {\tfa The author}
  \blank[2*medium]
    {\tfa July 26, 2005}
  \blank[3*medium]
\stopalignment

Then, the actual text of the document starts here.  We'll put in enough text to
fill out the line and start to make a paragraph.
\stoptext

In ConTeXt: A more reusable solution

The simple solution replicates the appearance of the LaTeX original, but it doesn't replicate the spirit of it, which is to separate the formatting from the content. We can do this by creating the following definitions, and putting them in a module file. The \title and similar commands are copied nearly directly from the LaTeX kernel, and the formatting command the same as the previous version -- note that we use \doctitle instead of \title, to avoid obliterating the \title command that ConTeXt is already using for unnumbered sections.

\unprotect
\def\doctitle#1{\gdef\@title{#1}}
\def\author#1{\gdef\@author{#1}}
\def\date#1{\gdef\@date{#1}}
\date{\currentdate}  % Default to today unless specified otherwise.

\def\maketitle{%
  \startalignment[center]
    \blank[force,2*big]
      {\tfd \@title}
    \blank[3*medium]
      {\tfa \@author}
    \blank[2*medium]
      {\tfa \@date}
    \blank[3*medium]
  \stopalignment}
\protect

Suppose that we save this in the file t-title.tex. Then, we can use this in our main ConTeXt document with the following syntax, which is quite similar to the LaTeX version aside from the addition of the \usemodule[title] command in place of \documentclass{article}.

\usemodule[title]
  \doctitle{How to do this in Context}
  \author{The author}
  \date{July 26, 2005}

\starttext
  \maketitle
  Then, the actual text of the document starts here.  We'll put in enough text to
  fill out the line and start to make a paragraph.
\stoptext

The typeset result is, of course, identical to the previous version. Now, the t-title.tex macros can be shared between any number of documents, and changes will affect all of the documents that use this file.

In ConTeXt: A more advanced solution

The next solution use an interface inspired by the ones from the core commands and tries to go away from hard coded values.

The values for the title are set with the \setuptitle command, it provides two arguments where the second argument is optional and could be used to change the layout for the three text blocks. The title block itself is placed with the \placetitle command.

\unprotect

\definesystemvariable {dt} % DocumentTitle

% Expand both 1- and 2- arity'd versions of /dosetuptitle
\def\setuptitle
  {\dodoubleempty\dosetuptitle}

% The one-argument iteration:
% For each comma separated item of arg1, define (dtcontent(arg1))...
% using \setuptitle in your document as defined at the bottom of this file

% The two-argument iteration:
% For each comma separated item of arg1, and using style/color in arg2...
% define (dt(arg1)(style)) and (dt(arg1)(color))

\def\dosetuptitle[#1][#2]%
  {\ifsecondargument
     \dodosetuptitle[#1][#2]%
   \else
     \dodosetuptitle[\v!content][#1]%
   \fi}

\def\dodosetuptitle[#1][#2]%
  {\def\dododosetuptitle##1%
     {\getparameters[\??dt##1][#2]}%
   \processcommalist[#1]\dododosetuptitle}

% Using earlier style/color definitions, apply style and color to arg1-item values
\def\placetitle
  {\startalignment[\v!middle]
   \blank[\v!force,2*\v!big]
   \doattributes{\??dt\c!title}\c!style\c!color\@@dtcontenttitle
   \blank[3*\v!medium]
   \doattributes{\??dt\c!author}\c!style\c!color\@@dtcontentauthor
   \blank[2*\v!medium]
   \doattributes{\??dt\c!date}\c!style\c!color\@@dtcontentdate
   \blank[3*\v!medium]
   \stopalignment}


% Here is where we define the key-value pairs
\setuptitle
  [\c!title]
  [\c!style=\tfd,
   \c!color=]

\setuptitle
  [\c!author,\c!date]
  [\c!style=\tfa,
   \c!color=]

\setuptitle
  [\c!title=,
   \c!author=,
   \c!date=\currentdate]

\protect

To reuse the definition above you could save the code in module like t-title.tex and load it in your document with \usemodule[title].

\setuptitle
  [title={How to do this in Context},
   author={The author},
   date={July 26, 2005}]

\starttext

\placetitle

Then, the actual text of the document starts here.  We'll put in enough text to
fill out the line and start to make a paragraph.

\stoptext

The definition for the macro \placetitle could be extended without problems to change the values for the space before and after the title block or the distance between all elements with the \setuptitle command.

Adding markings to headers and footers (in "A more reusable solution")

With just a few extra lines and two redefined definitions in this module, like so:

\unprotect
\definemarking[Author]
\definemarking[Doctitle]

\def\doctitle#1{\gdef\@title{#1}\marking[Doctitle]{#1}}
\def\author#1{\gdef\@author{#1}\marking[Author]{#1}}

% rest of the file as above

allow you to use the document title and author in your headers and footers:

\usemodule[title]

\doctitle{How to do this in Context}
\author{The author}
\date{July 26, 2005}

\setupheadertexts[{\getmarking[Doctitle]}][{\getmarking[Author]}]

\starttext
  \maketitle
  Then, the actual text of the document starts here.  We'll put in enough text to
  fill out the line and start to make a paragraph.
\stoptext



So, that's the title. The abstract can be handled very similarly; see Article Abstracts for details.