% \iffalse meta comment % File: nbaseprt.dtx Copyright (C) 2004 Harald Harders % \fi % % \iffalse % %<*driver> \listfiles \documentclass[ngerman,english,a4paper]{ltxdoc} \usepackage[T1]{fontenc} \IfFileExists{nbaseprt.sty}{% \usepackage{nbaseprt} }{% \GenericWarning{nbaseprt.dtx}{Package file nbaseprt.sty not found (Documentation will be messed up!^^J^^A Generate nbaseprt.sty by (La)TeXing numprint.ins, process nbaseprt.dtx again)^^J}\stop } \IfFileExists{eco.sty}{% \usepackage{eco} \renewcommand*\ttdefault{cmtt} }{% \GenericWarning{nbaseprt.dtx}{Package file eco.sty not found (Documentation will be less clear than possible)} } \usepackage{longtable} \usepackage{mathcomp} \usepackage{babel} \usepackage{url} \usepackage{color} \IfFileExists{booktabs.sty}{\usepackage{booktabs}}{% \GenericWarning{nbaseprt.dtx}{Package file booktabs.sty not found. Documentation formatting will be poor}% \def\toprule\hline \def\midrule\hline \def\bottomrule\hline} \GetFileInfo{nbaseprt.sty} \title{The nbaseprt package} \author{Harald Harders\\\texttt{harald.harders@gmx.de}} \date{Version \fileversion, \filedate; printed \today} \newlength{\tempdima}% \makeatletter \renewenvironment{table}[1][]{% \@float{table}[#1]% \centering% \setlength{\tempdima}{\abovecaptionskip}% \setlength{\abovecaptionskip}{\belowcaptionskip}% \setlength{\belowcaptionskip}{\tempdima}% }{% \end@float } \def\meta@font@select{\normalfont\itshape} \makeatother %% \EnableCrossrefs \CodelineIndex \DoNotIndex{\def,\edef,\let,\newcommand,\newenvironment,\newcounter} \DoNotIndex{\setcounter,\space,\if,\else,\fi,\empty,\@empty,\ifx,\fi} \DoNotIndex{\ifnum,\fi,\expandafter,\csname,\endcsname,\the} \DoNotIndex{\MessageBreak,\message,\newlength,\newif,\xdef,\newcount} \DoNotIndex{\begingroup,\endgroup,\,,\@tempcnta,\@tempdima,\advance} \DoNotIndex{\ensuremath,\filedate,\fileversion,\docdate} \DoNotIndex{\mathit,\mathrm,\mathsf,\nprt@tmp,\nprt@tmpnum,\relax} \DoNotIndex{\protect,\renewcommand,\setlength,\settowidth,\stepcounter} \DoNotIndex{\string} \CodelineNumbered \RecordChanges \CheckSum{343} \begin{document} \DocInput{nbaseprt.dtx} \end{document} % % \fi % % \changes{0.11}{2004/12/14}{Usage of \texttt{eco.sty} in % \texttt{nbaseprttest.tex} only if available}% % \changes{0.10}{2004/08/08}{Total new implementation}% % % \maketitle % \begin{abstract} % \noindent % This package prints integer numbers in different bases (octal, % decimal, hexadecimal, binary) similarly to the |numprint| package. % But here, the number of digits within one group depends on the % base. % % \textbf{This version of |nbaseprt.sty| is a BETA VERSION. % The main command \cs{nbaseprint} will stay stable but % all configuration commands and the output of % \cs{nbaseprint} may change in future. % Please give me feedback what can be improved and if the % abbreviations for the different number bases are correct.} % \end{abstract} % % \tableofcontents % % \section*{Copyright} % Copyright 2004 Harald Harders. % % This program can be redistributed and/or modified under the terms % of the LaTeX Project Public License Distributed from CTAN % archives in directory macros/latex/base/lppl.txt; either % version 1 of the License, or any later version. % % % \section{Load the package} % % To use this package place % \begin{verbatim} %\usepackage{nbaseprt}\end{verbatim} % in the preamble of your document. % The |nbaseprt| package calls the |numprint| package and parses all % package options to it. % Please read the documentation for the |numprint| package for % details. % If you want to use both the |numprint| and the |nbaseprt| package % either load |numprint| first given all options, e.g., % \begin{verbatim} %\usepackage[autolanguage,nosepfour]{numprint} %\usepackage{nbaseprt}\end{verbatim} % or only load the |nbaseprt| package, giving it the options for % |numprint|: % \begin{verbatim} %\usepackage[autolanguage,nosepfour]{nbaseprt}\end{verbatim} % % % \section{Print numbers} % % \DescribeMacro{\nbaseprint}% % Numbers are printed using the \cs{nbaseprint\marg{number}} command. % Which number base is used is determined by parsing the % \meta{number}. % % The type can be given by preceding the number by ``|0x|'', ``|0o|'', % ``|0d|'', or ``|0b|'' (or the uppercase characters) for hexadecimal, % octal, decimal, or binary numbers, respectively. % For example, % \begin{verbatim} %$\nbaseprint{0x1A0E3F}$, $\nbaseprint{0o377377}$, %$\nbaseprint{0d192314}$, $\nbaseprint{0b11010110}$\end{verbatim} % % Alternatively, hexadecimal and octal numbers can be given by % appending ``h'', ``H'', ``o'', or ``O'': % \begin{verbatim} %$\nbaseprint{1A0E3Fh}$, $\nbaseprint{377377o}$\end{verbatim} % % If neither is given, the number defaults to decimal. % % The format of the printed numbers is similar to the possible input % formats. % By default, the numbers are preceded by ``|0x|'', ``|0o|'', % ``|0d|'', or ``|0b|'', e.g. % % \begin{quotation} % $\nbaseprint{0x1A0E3F}$, $\nbaseprint{0o377377}$, % $\nbaseprint{0d192314}$, $\nbaseprint{0b11010110}$ % \end{quotation} % % \DescribeMacro{\nbaseposttext}% % You can change this by using \cs{nbaseposttext}. % This leads to % \begin{quotation} % \nbaseposttext % $\nbaseprint{0x1A0E3F}$, $\nbaseprint{0o377377}$, % $\nbaseprint{0d192314}$, $\nbaseprint{0b11010110}$ % \end{quotation} % % \DescribeMacro{\nbasepretext}% % You can switch back to the default behaviour using \cs{nbasepretext} % or by using \cs{nbaseposttext} inside a group. % % % If you want to print negative numbers the sign may be written before % or after ``|0x|'', ``|0o|'', ``|0d|'', or ``|0b|''. % Some examples: % \begin{verbatim} %$\nbaseprint{-0x1A0E3F}$, $\nbaseprint{0o-377377}$, %$\nbaseprint{0d+-192314}$, $\nbaseprint{0b\pm 11010110}$\end{verbatim} % which lead to % \begin{quotation} % $\nbaseprint{-0x1A0E3F}$, $\nbaseprint{0o-377377}$, % $\nbaseprint{0d+-192314}$, $\nbaseprint{0b\pm 11010110}$ % \end{quotation} % In the printout, the sign always is written after the base-specific % string. (is this correct?) % % % \section{Customization} % % \subsection{Padding a number on the left side}% % % \DescribeMacro{\nplpadding}% % \DescribeMacro{\npnolpadding}% % Sometimes it is desireable to have a number of a fixed length with % the missing digits filled with a character (mostly the character % ``0'', so this is the default). % This can be achieved calling % \cs{nplpadding\oarg{character}\marg{digits}} borrowed from the % |numprint| package. % For example, % \begin{verbatim} %\nplpadding{6}% %$\nbaseprint{0xA03E}$, $\nbaseprint{0o1234}$\end{verbatim} % leads to \begingroup % ``\nplpadding{6}$\nbaseprint{0xA03E}$, $\nbaseprint{0o1234}$'' % \endgroup % % \cs{npnolpadding} switches padding off. % % % \section{International support}% % \label{sec:international}% % % |nbaseprt| uses the thousand separator from |numprint|. % Since this package uses the German ``\cs{,}'' by default |nbaseprt| % does this, too. % Using the package option |autolanguage| this can be fixed. % If you are using this option without the |babel| package the % settings are switched to English at \cs{begin\{document\}}: % separator ``\texttt{,}''. % If using |babel| the separator is changed automatically when % switching to a supported language. % % If you do not want to use the |autolanguage| option you may use the % |numprint| command \cs{npthousandsep} command to change the % separator. % % % \section{Print aligned numbers in tabulars}% % \label{sec:tabular}% % % Sorry, not programmed, yet. % % % % \appendix % % \section{Lists of options and commands} % % This section contains lists of all package options resp.\ available % commands. % Items that belong together and may be exclusive are printed in % groups together. % % \subsection{Package options}% % \label{sec:package-options}% % % |nbaseprt| supports all options of the |numprint| package. % In this list, only the ones that are new or have a different meaning % are listed. % % \begingroup % \newcommand*\default{$^{*}$}% % The default values are marked by \default. % \begin{longtable}[l]{% % @{}>{\ttfamily}p{0.30\linewidth}@{}p{0.70\linewidth}@{}} % np& Define the shortcuts \cs{np} for \cs{numprint} and \cs{nbp} % for \cs{nbaseprint}. % \end{longtable} % \endgroup % % \subsection{Commands} % % Commands that begin with \cs{np} are borrowed from |numprint|. % Here, the new commands and |numprint| commands that have a special % meaning for |nbaseprt| are listed here. % % \begin{longtable}[l]{% % @{}>{\ttfamily \char`\\}p{0.3\linewidth}@{}p{0.7\linewidth}@{}} % npaddplus& Add a plus to a number without a sign.\\ % npnoaddplus& Don't do that.\\[\medskipamount] % nbp& Shortcut for \cs{nbaseprint} (only available with package % option~\texttt{np}).\\ % nbaseprint& Typesets a number (the package's main % command).\\[\medskipamount] % npthousandsep& Change the separator between the digit % groups.\\[\medskipamount] % nplpadding& Declare up to how many digits the number will be padded at % the lefthand side. \\ % npnolpadding& Switch off padding. \\[\medskipamount] % nbaserpretext& Switches on to precede the number by ``0x'', % ``0o'', or ``0d''. \\ % nbaserposttext& Switches on to append ``h'', ``o'', or nothing to % the number. % \end{longtable} % % % \section{To do} % % \begin{itemize} % \item Add table support. % \item Better customization for the pre and the post text. % \item Parse the argument for invalid numbers. % \item Proof output format of numbers. % \end{itemize} % % % \StopEventually{\PrintChanges \PrintIndex} % % \section{The implementation} % \iffalse %<*package> % \fi % Heading of the package: % \begin{macrocode} \NeedsTeXFormat{LaTeX2e} \ProvidesPackage{nbaseprt} [2004/12/14 v0.11 Print numbers with numerical bases (HH)] % \end{macrocode} % Warning, that this is a beta version. % \begin{macrocode} \typeout{^^J*************************************************************} \PackageWarningNoLine{nbaseprt}{This version of nbaseprt.sty is a BETA VERSION.\MessageBreak The main command \string\nbaseprint\space will stay stable but\MessageBreak all configuration commands and the output of\MessageBreak \string\nbaseprint\space may change in future.\MessageBreak Please give me feedback what can be improved and if\MessageBreak the abbreviations for the different number bases are\MessageBreak correct} \typeout{*************************************************************^^J} % \end{macrocode} % Pass all unknown options to |numprint.sty| to avoid conflicts when % loading |numprint| seperately. % \begin{macrocode} \DeclareOption{np}{% \newcommand*\nbp{\nbaseprint}% \PassOptionsToPackage{np}{numprint}% } \DeclareOption*{\PassOptionsToPackage{\CurrentOption}{numprint}} \ProcessOptions\relax % \end{macrocode} % Load package |numprint| because |nbaseprt| shares some commands with % it. % \begin{macrocode} \RequirePackage{numprint} % \end{macrocode} % Easier if-then clauses. % \begin{macrocode} \RequirePackage{ifthen} % \end{macrocode} % Dummy definitions to get an error in case of incompatibility with % other packages. % \begin{macrocode} \newcommand*\nbprt@base{} \newcommand*\nbprt@number{} % \end{macrocode} % \begin{macro}{\nbprt@testbase} % Test which numeric base is used in the argument of \cs{nbaseprt}. % \begin{macrocode} \newcommand*\nbprt@testbase{} \def\nbprt@testbase#1#2#3\@empty{% % \end{macrocode} % Test if number begins with ``|0x|'', ``|0X|'', or ``|"|'' which all mean % that it is given as hexadecimal number. % \begin{macrocode} \ifthenelse{\equal{#1#2}{0x}\or\equal{#1#2}{0X}\or\equal{#1}{"}}{% \gdef\nbprt@base{hex}% % \end{macrocode} % Store the rest of the argument as number in \cs{nbprt@number}. % \begin{macrocode} \ifthenelse{\equal{#1}{"}}{% \edef\nbprt@number{#2#3}% }{% \edef\nbprt@number{#3}% }% }{% % \end{macrocode} % Test if number begins with ``|0o|'', ``|0O|'', or ``|'|'' which all % mean that it is given as octal number. % \begin{macrocode} \ifthenelse{\equal{#1#2}{0o}\or\equal{#1#2}{0O}\or\equal{#1}{'}}{% \gdef\nbprt@base{oct}% % \end{macrocode} % Store the rest of the argument as number in \cs{nbprt@number}. % \begin{macrocode} \ifthenelse{\equal{#1}{'}}{% \edef\nbprt@number{#2#3}% }{% \edef\nbprt@number{#3}% }% }{% % \end{macrocode} % Test if number begins with ``|0d|'' or ``|0D|'' which means that it % is given as decimal number. % \begin{macrocode} \ifthenelse{\equal{#1#2}{0d}\or\equal{#1#2}{0D}}{% \gdef\nbprt@base{dec}% % \end{macrocode} % Store the rest of the argument as number in \cs{nbprt@number}. % \begin{macrocode} \edef\nbprt@number{#3}% }{% % \end{macrocode} % Test if number begins with ``|0b|'' or ``|0B|'' which means that it % is given as decimal number. % \begin{macrocode} \ifthenelse{\equal{#1#2}{0b}\or\equal{#1#2}{0B}}{% \gdef\nbprt@base{bin}% % \end{macrocode} % Store the rest of the argument as number in \cs{nbprt@number}. % \begin{macrocode} \edef\nbprt@number{#3}% }{% % \end{macrocode} % If none of the above is the case the number defaults to decimal. % \begin{macrocode} \def\nbprt@base{dec}% \edef\nbprt@number{#1#2#3}% % \end{macrocode} % But there are also other possibilities to mark the number as % hexadecimal or octal, by appending ``|h|'', ``|H|'', ``|o|'', or % ``|O|''. % These tests are performed by seperate macros. % \begin{macrocode} \nbprt@ishex#1#2#3h\@empty\@empty \nbprt@isHex#1#2#3H\@empty\@empty \nbprt@isoct#1#2#3o\@empty\@empty \nbprt@isOct#1#2#3O\@empty\@empty }% }% }% }% % \end{macrocode} % Test for a sign before the number. % \begin{macrocode} \expandafter\nbprt@testsign\nbprt@number\@empty\@empty\@empty % \end{macrocode} % Reset \cs{nbprt@string} that holds the number in formatted form. % \begin{macrocode} \def\nbprt@string{}% % \end{macrocode} % Reset the counters that help formatting the number. % \begin{macrocode} \@tempcnta=0 \@tempcntb=0 % \end{macrocode} % Parse the number, done by \cs{nbprt@parsenum}. % \begin{macrocode} \expandafter\nbprt@parsenum\nbprt@number\@empty % \end{macrocode} % If left padding is switched on, add the leading characters to gain % the specified length. % See \cs{nbprt@parsenum} for explanation of the algorithm. % \begin{macrocode} \whiledo{\the\@tempcntb<\nprt@lpaddigits}{% \ifnum\@tempcnta=\csname nbprt@digitgroup@\nbprt@base\endcsname\relax \edef\nbprt@string{\nprt@separator@before\nbprt@string}% \@tempcnta=0 \fi \edef\nbprt@string{\nprt@lpadchar\nbprt@string}% \advance\@tempcntb 1 \advance\@tempcnta 1 }% % \end{macrocode} % Print the text that marks the base of the number before the number % itself. % \begin{macrocode} \ifnbprt@pretext \csname nbprt@pretext@\nbprt@base\endcsname \nbprt@presep \fi % \end{macrocode} % Print the sign (use routine of |numprint|). % \begin{macrocode} \nprt@printsign{mantissa}\nbprt@sign\@empty % \end{macrocode} % Print the modified number with separators. % \begin{macrocode} \nbprt@string % \end{macrocode} % Print the text that marks the base of the number after the number % itself. % \begin{macrocode} \ifnbprt@pretext \else \nbprt@postsep \csname nbprt@posttext@\nbprt@base\endcsname \fi } % \end{macrocode} % \end{macro} % \begin{macro}{\nbprt@testsign} % \begin{macrocode} \def\nbprt@testsign#1#2#3\@empty{% % ``#1'', ``#2'', ``#3'': \nprt@IfCharInString{#1}{\nprt@signlist}{% \edef\nbprt@number{#2#3}% \edef\nbprt@sign{#1}% \ifx\nbprt@sign\nprt@plus@test \def\nprt@tmp{#2}% \ifx\nprt@tmp\nprt@minus@test \edef\nbprt@sign{+-}% \edef\nbprt@number{#3}% \fi \else \ifx\nbprt@sign\nprt@plusminus@test \edef\nbprt@sign{+-}% \fi \fi }{% \edef\nbprt@number{#1#2#3}% }% } % \end{macrocode} % \end{macro} % Test if the number is marked as hexadecimal by appending an ``|h|''. % \begin{macrocode} \def\nbprt@ishex#1h#2\@empty{% % \end{macrocode} % If |#2| is |h|, the number has ended with an |h| because this macro % has been called with an appended |h| in addition to the |h| that is % the last character of the number. % \begin{macrocode} \ifthenelse{\equal{#2}{h}}{% % \end{macrocode} % Set the base and redefine the number. % \begin{macrocode} \def\nbprt@base{hex}% \edef\nbprt@number{#1}% }{}% } % \end{macrocode} % Test if the number is marked as hexadecimal by appending an ``|H|''. % \begin{macrocode} \def\nbprt@isHex#1H#2\@empty{% \ifthenelse{\equal{#2}{H}}{% \def\nbprt@base{hex}% \edef\nbprt@number{#1}% }{}% } % \end{macrocode} % Test if the number is marked as octal by appending an ``|o|'' or an % ''|O|''. % \begin{macrocode} \def\nbprt@isoct#1o#2\@empty{% \ifthenelse{\equal{#2}{o}}{% \def\nbprt@base{oct}% \edef\nbprt@number{#1}% }{}% } \def\nbprt@isOct#1O#2\@empty{% \ifthenelse{\equal{#2}{O}}{% \def\nbprt@base{oct}% \edef\nbprt@number{#1}% }{}% } % \end{macrocode} % \begin{macro}{\nbprt@parsenum} % Parses the given number and generates the formatted string in % \cs{nbprt@string}, working recursively. % |#1| is the first character in the left number, |#2| is the rest. % \begin{macrocode} \def\nbprt@parsenum#1#2\@empty{% % \end{macrocode} % If |#2| is not \cs{@empty} call \cs{nbprt@parsenum} recursively to % parse the number backwards. % \begin{macrocode} \ifthenelse{\equal{#2}{\@empty}}{}{% \expandafter\nbprt@parsenum#2\@empty }% % \end{macrocode} % Test if \cs{@tempcnta} has reached the number of digits that are % printed as group for the given number base (stored in % \cs{nbprt@digitgroup@}\meta{\cs{nbprt@base}}). % \begin{macrocode} \ifnum\@tempcnta=\csname nbprt@digitgroup@\nbprt@base\endcsname\relax % \end{macrocode} % Precede the formatted number by the separator % \cs{nprt@separator@before}, taken from |numprint.sty|. % \begin{macrocode} \edef\nbprt@string{\nprt@separator@before\nbprt@string}% % \end{macrocode} % Reset the number of handled characters in this group. % \begin{macrocode} \@tempcnta=0 \fi % \end{macrocode} % Precede the formatted number by the current character while forcing % uppercase hexadecimal numbers. % \begin{macrocode} \edef\nbprt@string{% \uppercase{\ifmmode\mathrm{#1}\else#1\fi}% \nbprt@string}% % \end{macrocode} % Count this digit for the current group (\cs{@tempcnta}) and for the % total number of digits (\cs{@tempcntb}). % \begin{macrocode} \advance\@tempcntb 1 \advance\@tempcnta 1 } % \end{macrocode} % \end{macro} % \begin{macro}{\nbasepretext} % Provide a command that switches to marking the numbers before the % number itself. % \begin{macrocode} \newif\ifnbprt@pretext \newcommand*\nbasepretext{\nbprt@pretexttrue} % \end{macrocode} % \end{macro} % \begin{macro}{\nbaseposttext} % Provide a command that switches to marking the numbers after the % number itself. % \begin{macrocode} \newcommand*\nbaseposttext{\nbprt@pretextfalse} % \end{macrocode} % \end{macro} % Provide the commands that print the text before or after the number. % \begin{macrocode} \def\nbprt@pretext@hex{0\ifmmode\mathrm{x}\else x\fi}% \def\nbprt@pretext@oct{0\ifmmode\mathrm{o}\else o\fi}% \def\nbprt@pretext@dec{0\ifmmode\mathrm{d}\else d\fi}% \def\nbprt@pretext@bin{0\ifmmode\mathrm{b}\else b\fi}% \def\nbprt@presep{\,}% \def\nbprt@posttext@hex{\ifmmode\mathrm{h}\else h\fi}% \def\nbprt@posttext@oct{\ifmmode\mathrm{o}\else o\fi}% \def\nbprt@posttext@dec{\ifmmode\mathrm{d}\else d\fi}% \def\nbprt@posttext@bin{\ifmmode\mathrm{b}\else b\fi}% \def\nbprt@postsep{\,}% % \end{macrocode} % By default, use the marker before the number. % \begin{macrocode} \nbasepretext % \end{macrocode} % Define how many numbers are grouped together, depending on the % number base. % \begin{macrocode} \def\nbprt@digitgroup@hex{2}% \def\nbprt@digitgroup@oct{3}% \def\nbprt@digitgroup@dec{3}% \def\nbprt@digitgroup@bin{4}% % \end{macrocode} % \begin{macro}{\nbaseprint} % Define the man command \cs{nbaseprint} which takes the printed % number as mandatory argument. % \begin{macrocode} \DeclareRobustCommand*\nbaseprint[1]{% % \end{macrocode} % First, expand the number to allow to use macros in the argument. % \begin{macrocode} \edef\nbprt@number{#1}% % \end{macrocode} % Test if the number begins with a sign. % \begin{macrocode} \def\nbprt@sign{}% \expandafter\nbprt@testsign\nbprt@number\@empty\@empty\@empty % \end{macrocode} % Call \cs{nbprt@testbase} which tests for the number base and prints % the number. % \begin{macrocode} \expandafter\nbprt@testbase\nbprt@number\@empty\@empty\@empty } % \end{macrocode} % \end{macro} % % \iffalse % % \fi % \Finale