4

I'm preparing a document class:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myclass}
\LoadClass{book}
\RequirePackage[demo]{graphicx}
\newcommand{\@mypic}[1]{%
\includegraphics[width=.3\textwidth]{pic1}
}

\newcommand{\mytitlepage}{
\begin{center}
Title 

\@mypic


Jan 2015
\end{center}
}

And this is my root file:

\documentclass{myclass}
\begin{document}
\mytitlepage
\end{document}

My problem is with the \@mypic command. I want this command to load the pic1 figure by default, but also I want that my users be able to load their figure in the preamble, if they don't like the default figure. In other words, I want this: If they enter a command like \mypic[.6]{pic3} in the preamble, the pic3 substitute for the pic1 with .6\textwidth.
How can I do this?

Edit (More clear explanation):

I want this:
If my users don't like the default figure and its width, they can change both the figure and its width. I think this problem has a simple solution.
BTW, it would be better the solution use only latex core, not using packages like xparse.

  • Should just the file name be optional or the width as well? Do you want a single command to set this data or can we use two separate ones (e.g. \titlepicture and \titlepicturewidth or similar)? – Joseph Wright Jan 20 '15 at 14:40
  • @JosephWright: please see my Edit above. I just want my users to be able to use their figure and their desired width. – Sisabe Jan 20 '15 at 19:50
  • @Sisabe I added a version of sisabe.cls that uses only features already loaded by graphicx. You lose much flexibility, though. – egreg Jan 20 '15 at 21:25
4

I'd use a key-value syntax, for example the one provided by expl3:

File sisabe.cls

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{sisabeclass}
\LoadClass{book}

\RequirePackage{graphicx,xparse}

\ExplSyntaxOn
\NewDocumentCommand{\mytitlepage}{ O{} }
 {
  \begin{center}
  \keys_set:nn { sisabeclass / titlepage } { #1 }
  \g_sisabe_titlepage_title_tl
  \par
  \vspace{1cm} % adjust
  \sisabe_titlepage_picture:VV \l_sisabe_titlepage_picoptions_tl \l_sisabe_titlepage_picture_tl
  \par
  \vspace{1cm} % adjust
  \g_sisabe_titlepage_date_tl
  \end{center}
 }

\keys_define:nn { sisabeclass / titlepage }
 {
  title   .tl_gset:N = \g_sisabe_titlepage_title_tl,
  title   .initial:n = MISSING TITLE,
  date    .tl_gset:N = \g_sisabe_titlepage_date_tl,
  date    .initial:n = MISSING DATE,
  options .tl_set:N  = \l_sisabe_titlepage_picoptions_tl,
  options .initial:n = { width=.3\textwidth },
  picture .tl_set:N  = \l_sisabe_titlepage_picture_tl,
  picture .initial:n = { example-image.pdf }, % adjust
 }

\cs_new_protected:Npn \sisabe_titlepage_picture:nn #1 #2
 {
  \includegraphics[#1]{#2}
 }

\cs_generate_variant:Nn \sisabe_titlepage_picture:nn { VV }

\ExplSyntaxOff

\endinput

File test.tex

\documentclass{sisabe}

\begin{document}

\mytitlepage

\hrule

\mytitlepage[
  title = A wonderful paper,
  date = January 2015,
  picture = example-image-9x16.pdf,
  options = {height=4cm},
]

\end{document}

Output

enter image description here


File sisabe.cls that only uses features already loaded by graphicx

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{sisabeclass}
\LoadClass{book}

\RequirePackage{graphicx} % already loads keyval

\newcommand{\mytitlepage}[1][]{%
  \begin{center}
  \setkeys{sisabetitle}{#1}
  \sisabetitlepage@title
  \par
  \vspace{1cm} % adjust
  \begingroup\edef\x{\endgroup
    \noexpand\includegraphics[\sisabetitlepage@picoptions]{\sisabetitlepage@picture}%
  }\x
  \par
  \vspace{1cm} % adjust
  \sisabetitlepage@date
  \end{center}
 }

\define@key{sisabetitle}{title}{\gdef\sisabetitlepage@title{#1}}
\define@key{sisabetitle}{date}{\gdef\sisabetitlepage@date{#1}}
\define@key{sisabetitle}{options}{\def\sisabetitlepage@picoptions{#1}}
\define@key{sisabetitle}{picture}{\def\sisabetitlepage@picture{#1}}
\setkeys{sisabetitle}{%
  title=MISSING TITLE,
  date=MISSING DATE,
  options={width=.3\textwidth},
  picture={example-image.pdf},% adjust
 }
\endinput

The output with the same test file is exactly the same.

2

This provides two possible ways to define the command with some optional argument -- the classical \newcommand style and with \NewDocumentCommand. If the optional argument is omitted, the pic1 is used,otherwise the value of #1 as optional argument.

More configurability could be achieved with a key-value interface.

Class:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myclass}
\LoadClass{book}
\RequirePackage{graphicx}

\RequirePackage{xparse}

\newcommand{\@mypic}[1]{%
  \includegraphics[width=.3\textwidth]{#1}%
}




\newcommand{\mytitlepage}[1][pic1]{
\begin{center}
Title  

\@mypic{#1}%


Jan 2015
\end{center}
}



\NewDocumentCommand{\MyTitlePage}{+O{pic1}}{%
\begin{center}
Title NewDocumentCommand-like 

\@mypic{#1}

\today
\end{center}
}%

Document:

\documentclass{myclass}

\begin{document}
\mytitlepage
\mytitlepage[bild_erwin_schroedinger]

\MyTitlePage[bild_erwin_schroedinger]

\MyTitlePage
\end{document}
  • And what about the figure width? I want my users can change the both figure and its width (see my Edit above). – Sisabe Jan 20 '15 at 19:53
  • @Sisabe: I've written in my solution that a better approach would be a key-value version, but I had no time this afternoon to implement this. In the meantime, egreg provided such a solution, so it's not really useful to update my solution, unless you 'insist' – user31729 Jan 20 '15 at 21:40
1

I have done it with a standard class, but you can implement these macros into your class. I make \mytitlepage have two optional arguments. The first must be the resize and the second is the image replacement.

So, if you want a new image, you must specify the size fraction argument. However, I have EDITED the answer so that a blank first optional argument, as in \mytitlepage[][example-image-B] will retain the default size of .3\textwidth.

\documentclass{article}
\RequirePackage{graphicx}
\makeatletter
\newcommand{\@mypic}[2]{\includegraphics[width=#1\textwidth]{#2}}
\newcommand{\mytitlepage}[1][.3]{%
  \ifx\relax#1\relax\def\figwidth{.3}\else\def\figwidth{#1}\fi%
  \mytitlepagehelp}
\newcommand{\mytitlepagehelp}[1][example-image-A]{%
\begin{center}
Title\par
\@mypic{\figwidth}{#1}\par
Jan 2015
\end{center}
}
\makeatother
\begin{document}
\mytitlepage
\mytitlepage[.4]
\mytitlepage[.4][example-image-B]
\mytitlepage[][example-image-C]
\end{document}

enter image description here

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.