%==================================================
% kakenhi.sty
%==================================================
% Minimum amount of macros for writing Kakenhi forms.
% Imported from Kakenhi Macro at
% http://www.yukawa.kyoto-u.ac.jp/contents/researcher/kakenhi.html .
%
% 2005-10-24 Taku Yamanaka, Physics Dept. Osaka Univ.
%		taku@hep.sci.osaka-u.ac.jp
% 2006-06-04 Taku
%		Added macros to draw boxes if \DrawBox is in the source.
%		This is useful when designing the LaTeX forms.
% 2006-06-14 Taku
%		Added LaTeX macros to add costs 
%		(\KLResetGrandSum, \KLCostItem, \KLSum, \KLGrandSum).
%		Added a macro \Number to supply commas every 3 digits (imported from kkh.mac).
% 2006-06-25 Taku
%		Added \KLTabC, \KLTabR, \KLTabRNum to specify alignments in tables.
%		Please note that \phantom{...} is required for the column, 
%		or otherwise somehow p{...mm} is ignored.
% 2006-08-13 Taku
%		Added \KLItemNumUnitCost . This requires calc.sty.
% 2006-09-06 Taku
%		Added KLGrandTotalValue to add ALL the costs.
%		Also added \KLPrintGrandTotal to print the total on the console.
% 2006-09-09 Taku
%		Added macros to handle "efforts".
% 2006-09-10 Taku
%		Added \KLnewcounter to make series of counters.
%		Modified \KLResetGrandSum and \KLSum to add the sum for 
%		each category and year.
% 2006-09-11 Taku
%		Added \NumC to display LaTeX counter with commas.
% 2006-09-12 Taku
%		Added simple macros to make group table.
% 2006-09-23 Taku
%		Added the 'Fair License' notification.
%==================================================

\newdimen\X
\newdimen\Y
\newdimen\XB
\newdimen\XA    % also used as \htbackskip
\newdimen\YA    % also used as \dpbackskip, \upperlimit
\newdimen\XM \XM=0mm% X-direction margin
\newdimen\YM \YM=0mm% Y-direction margin
\newdimen\rulelength
\newdimen\ruleX
\newdimen\ruleY

\newbox\dummybox

%
%magic way of pointing a place defined by X-Y coordinate
\let\htbackskip=\XA
\let\dpbackskip=\YA

\newif\ifDrawBox\DrawBoxfalse\def\DrawBox{\DrawBoxtrue}

%----- XY ----------------------------------
\long\def\XY#1#2#3{{
  \setbox\dummybox=\vbox{
    \vskip#2
    \hbox to \hsize{%
      \hskip#1
      \vbox{\advance\hsize by -#1#3}\hfil
    }
  }
  \htbackskip=-\ht\dummybox
  \dpbackskip=-\dp\dummybox
%
  \box\dummybox\nointerlineskip
  \vskip\htbackskip
  \vskip\dpbackskip
}}
%

%------ DrawHLineX --------------------
% Draw a horizontal line from (#1, #2) to (#3, #2).
% Originally \Hrulethin in Kakenhi Macro.
\long\def\DrawHLineX#1#2#3{{
   \rulelength=#3
   \advance\rulelength by -#1
   \XY{#1}{#2}{\vrule width\rulelength height0.3pt depth0pt}
%    \rulelength=#3
%    \advance\rulelength by -#1
%    \XY{#1}{#2}{\vrule width\rulelength height0.3pt depth0pt}
}}

%------ DrawVLineY --------------------
% Draw a vertical line from (#1, #2) to (#1, #3)
% Originally \Vrulethin in Kakenhi Macro
\def\DrawVLineY#1#2#3{
    \rulelength=#3
    \advance\rulelength by -#2
    \XY{#1}{#2}{\vrule width0.3pt height\rulelength depth0pt}
}

%------ DrawHLineW -------------------
% Draw a horizontal line from (#1, #2) with a width #3.
\def\DrawHLineW#1#2#3{
    \XY{#1}{#2}{\vrule width#3 height0.3pt depth0pt}
}

%------- DrawVLineH -------------------
% Draw a vertical line from (#1, #2) with a height #3.
\def\DrawVLineH#1#2#3{
    \XY{#1}{#2}{\vrule width0.3pt height#3 depth0pt}
}

%------ DrawBoxXY ---------------------
% Draw a box with corners (#1, #2) and (#3, #4).
\long\def\DrawBoxXY#1#2#3#4{
    \DrawHLineX{#1}{#2}{#3}
    \DrawVLineY{#1}{#2}{#4}
    \DrawHLineX{#1}{#4}{#3}
    \DrawVLineY{#3}{#2}{#4}
}

%----- DrawBoxWH ---------------------
% Draw a box; top-left corner at (#1, #2), width #3, height #4.
\long\def\DrawBoxWH#1#2#3#4{
    \ruleX=#1
    \advance\ruleX by #3
    \ruleY=#2
    \advance\ruleY by #4
    \DrawHLineW{#1}{#2}{#3}
    \DrawHLineW{#1}{\ruleY}{#3}
    \DrawVLineH{#1}{#2}{#4}
    \DrawVLineH{\ruleX}{#2}{#4}
}

%------ XYBC -----------------------------
% Make a box defined by coordinates
% (#1, #2): top-left and
% (#3, #4): bottom-right,
% and put contents #5 centered in the box.
% Imported from Kakenhi Macro.
\long\def\XYBC#1#2#3#4#5{
  \ifDrawBox
  	\DrawBoxXY{#1}{#2}{#3}{#4}
  \fi
  \X=#3 \Y=#4 \advance\X by -#1 \advance\Y by -#2
  \XY{#1}{#2}{\CBOX{\X}{\Y}{#5}}
}
%------ CBOX --------------------------------
% Center contents in the box.
% Imported from Kakenhi Macro.
\long\def\CBOX#1#2#3{{
  \hsize=#1\linewidth=\hsize
  \setbox\dummybox\hbox{#3}
  \vbox to #2{
    \vfil
    \ifdim#1 < \wd\dummybox
      \X=#1
      \advance\X by -4mm
      \hbox to \hsize{\hfil\vbox{\hsize=\X\linewidth=\hsize #3}\hfil}
    \else
      \hbox to \hsize{\hfil #3\hfil}
    \fi
    \vfil
  }
}}

%------ XYBP ----------------------------------
% Make a box defined by coordinates
% (#1, #2): top-left and
% (#3, #4): bottom-right,
% and put text #5 in the box as a paragraph with indentation.
% Margins are defined by XM and YM.
% Imported from Kakenhi Macro.
\long\def\XYBP#1#2#3#4#5{
  \ifDrawBox
  	\DrawBoxXY{#1}{#2}{#3}{#4}
  \fi
  \X=#3 \Y=#4 \advance\X by -#1 \advance\Y by -#2
  \XY{#1}{#2}{\PBOX{\X}{\Y}{\setlength{\parindent}{1zw}#5}}
}

%------ XYBLJ ----------------------------------
% Make a box defined by coordinates
% (#1, #2): top-left and
% (#3, #4): bottom-right,
% and put text #5 in the box, left-justified.
% Margins are defined by XM and YM.
\long\def\XYBLJ#1#2#3#4#5{
  \ifDrawBox
  	\DrawBoxXY{#1}{#2}{#3}{#4}
  \fi
  \X=#3 \Y=#4 \advance\X by -#1 \advance\Y by -#2
  \XY{#1}{#2}{\PBOX{\X}{\Y}{#5}}
}

%---- PBOX --------------------------------------
\long\def\PBOX#1#2#3{
   \X=#1 \Y=#2 \advance\X by -\XM \XA=\X \advance\X by -\XM
               \advance\Y by -\YM \YA=\Y \advance\Y by -\YM
   \vbox to \YA{\vskip\YM
   \hbox to \XA{\hskip\XM\vbox to \Y{\hsize=\X\linewidth=\X #3\vfil}%
   \hfil}
}}

%-------- WHBC --------------------------------
%	2005-10-24 Taku Yamanaka
%.............................................................................
% Make a box defined by
% (#1, #2): top-left,
% width #3,
% height #4, 
% and put text #5 centered in the box.
% Margins are defined by XM and YM.
\long\def\WHBC#1#2#3#4#5{
  \ifDrawBox
  	\DrawBoxWH{#1}{#2}{#3}{#4}
  \fi
  \XY{#1}{#2}{\CBOX{#3}{#4}{#5}}
}

%-------- WHBP --------------------------------
%	2006-05-28 Taku Yamanaka
%.............................................................................
% Make a box defined by 
% (#1, #2): top-left,
% width #3, 
% height #4,
% and put text #5 in the box as a paragraph with indentation.
% Margins are defined by XM and YM.
\long\def\WHBP#1#2#3#4#5{
  \ifDrawBox
  	\DrawBoxWH{#1}{#2}{#3}{#4}
  \fi
  \XY{#1}{#2}{\PBOX{#3}{#4}{\setlength{\parindent}{1zw}#5}}
}

%-------- WHBLJ --------------------------------
%	2005-10-24 Taku Yamanaka
%.............................................................................
% Make a box defined by 
% (#1, #2): top-left,
% width #3, 
% height #4,
% and put text #5 in the box, left-justified.
% Margins are defined by XM and YM.
\long\def\WHBLJ#1#2#3#4#5{
  \ifDrawBox
  	\DrawBoxWH{#1}{#2}{#3}{#4}
  \fi
  \XY{#1}{#2}{\PBOX{#3}{#4}{#5}}
}

%=====================================================
% Macro to supply commas every 3 digits (up to 9 digits)
%	Imported from kkh.mac for Kakenhi Macro.
%=====================================================
%
\newif\ifNumWithCommas \NumWithCommastrue
\def\NumWithCommas{\NumWithCommastrue}
\def\NumWithoutCommas{\NumWithCommasfalse}
\newcount\Numa
\newcount\Numb
\def\Numempty{}%output blank if "-0" is given
\def\Number#1{\edef\Numpar{#1}\ifx\Numempty\Numpar\else%
\ifNumWithCommas\Numa=#1\relax
\ifnum\Numa>999999\divide\Numa by 1000000
\number\Numa,%
\multiply\Numa by -1000000\advance\Numa by #1\relax
\Numb=\Numa\divide\Numa by 1000
\ifnum\Numa<100 \ifnum\Numa<10 0\fi0\fi\number\Numa,%
\multiply\Numa by -1000\advance\Numa by \Numb
\ifnum\Numa<100 \ifnum\Numa<10 0\fi0\fi\number\Numa%
\else\ifnum\Numa>999\divide\Numa by 1000
\number\Numa,%
\multiply\Numa by -1000\advance\Numa by #1\relax
\ifnum\Numa<100 \ifnum\Numa<10 0\fi0\fi\number\Numa%
\else\number\Numa\fi\fi\else\number#1\fi\fi}

%======================================================
% Macro to display LaTeX counter with commas every 3 digits.
%======================================================
\newcommand{\NumC}[1]{\Number{\value{#1}}}

%======================================================
% Macros to align (right-justify, center) elements within a tabular cell
% whose width is defined by p{...}.
% 2006-06-25 Taku
% 	These are necessary, because the cell width should be given explicitly
% 	by p{...mm} to match the given table in a tabular environment.  
% 	One could allocate a width with \phantom{...},
% 	but it is a little tricky, since it depends on the font size.
%======================================================

%---------------------------------------------------------------------
% Center text within a tabular cell allocated by p{...}
\newcommand{\KLTabC}[1]{\multicolumn{1}{c}{#1}}

%---------------------------------------------------------------------
% Right justify text within a tabular cell allocated  by p{...}
\newcommand{\KLTabR}[1]{\multicolumn{1}{r@{\ }}{#1}}

%---------------------------------------------------------------------
% Right justify number (with comma every 3 digits) 
% within a tabular cell allocated by p{...}
\newcommand{\KLTabRNum}[1]{\KLTabR{\Number{#1}}}

%=================================================
%  counter tools
%=================================================
\newcounter{KLtmp}

%------------------------------------------------------------------------------
% Makes a set of counters, with prefix #1, followed by 
% suffix ranging from 0 to #2 - 1.
% For example, \KLnewcounter{mine}{3} makes counters
% mine0, mine1, and mine2 .
%-------------------------------------------------------------------------------
\newcommand{\KLnewcounter}[2]{
	\setcounter{KLtmp}{0}
	
	\whiledo{\value{KLtmp} < #2}{
		\newcounter{#1\arabic{KLtmp}}
		\stepcounter{KLtmp}
	}
}

%------------------------------------------------------------------------------
% Dumps the contents of the counters.
%------------------------------------------------------------------------------
\newcommand{\KLdumpcounter}[2]{
	\setcounter{KLtmp}{0}
	
	\whiledo{\value{KLtmp} < #2}{
		#1\arabic{KLtmp} : \arabic{#1\arabic{KLtmp}}\\
		\stepcounter{KLtmp}
	}
}

%=======================================================
% LaTeX macros to add costs.
%	2006-06-14 Taku Yamanaka
%=======================================================
\newcounter{KLCost}				% to calculate cost = #units x unit cost
\newcounter{KLSumValue}			% for each year
\newcounter{KLGrandSumValue}		% for the grand sum (summed over multiple years)
\newcounter{KLGrandTotalValue}		% for the grand total of all the categories in all years
\setcounter{KLGrandTotalValue}{0}

\newcommand{\KLCostCategory}{KLequipments}
\newcounter{KLYearCount}

% Make counters for annual sums for each category-----------------------
\newcommand{\KLMaxYear}{8}
\KLnewcounter{KLequipments}{\KLMaxYear}
\KLnewcounter{KLexpendables}{\KLMaxYear}
%\KLnewcounter{KLdomestic}{\KLMaxYear}
%\KLnewcounter{KLforeign}{\KLMaxYear}
\KLnewcounter{KLtravel}{\KLMaxYear}
\KLnewcounter{KLgratitude}{\KLMaxYear}
\KLnewcounter{KLmisc}{\KLMaxYear}
\KLnewcounter{KLAnnualSum}{\KLMaxYear}

%#######################################################
% \KLResetGrandSum should be called BEFORE \begin{tabular}
% to reset the sum values.
%#######################################################
% Do not remove "%" after following lines, or otherwise extra blanks will
% appear in the table.
\newcommand{\KLResetGrandSum}{%
	\setcounter{KLSumValue}{0}%
	\setcounter{KLGrandSumValue}{0}%
	\setcounter{KLYearCount}{0}%
}

\newcommand{\KLResetSum}{%
	\setcounter{KLSumValue}{0}%
}

%--------------------------------------------------------------------------------
% Show the item and cost in a table.  Cost will be added to the sums.
% Usage example:
%	\KLItemCost{Expensive item}{12345678}
%--------------------------------------------------------------------------------
\newcommand{\KLItemCost}[2]{#1 & \KLTabR{\Number{#2}}%
	\addtocounter{KLSumValue}{#2}%
	\addtocounter{KLGrandSumValue}{#2}%
	\addtocounter{KLGrandTotalValue}{#2}%
	\\
}

%---------------------------------------------------------------------------------
% Show item, the number of units, unit cost, and overall cost in a table.
% The cost is calculated by the number of units * unit cost, automatically.
% The overall cost will be added to the sums.
% Usage example:
%	\KLItemNumUnitCost{cheap item}{100}{20}%
%    The percentage character at the end avoids extra blank after the calculated cost.
%---------------------------------------------------------------------------------
\newcommand{\KLItemNumUnitCost}[3]{%
	\setcounter{KLCost}{#2*#3}%
	#1 & \KLTabR{#2} & \KLTabR{\Number{#3}} & \KLTabR{\Number{\value{KLCost}}}%
	\addtocounter{KLSumValue}{\value{KLCost}}%
	\addtocounter{KLGrandSumValue}{\value{KLCost}}%
	\addtocounter{KLGrandTotalValue}{\value{KLCost}}%
}

%--------------------------------------------------------------------
% Display the sum, and reset the value.
% Also, the sum will be stored for each category/year, and in the annual sum. (2006-09-10)
% Usage example in tabular:
%	\hline
%	Total & \KLSum \\
%	\hline
%--------------------------------------------------------------------
\newcommand{\KLSum}{%
	\KLTabR{\Number{\arabic{KLSumValue}}}%
	\stepcounter{KLYearCount}%
	\addtocounter{\KLCostCategory0}{\value{KLSumValue}}%
	\addtocounter{\KLCostCategory\arabic{KLYearCount}}{\value{KLSumValue}}%
	\addtocounter{KLAnnualSum\arabic{KLYearCount}}{\value{KLSumValue}}%
	\addtocounter{KLAnnualSum0}{\value{KLSumValue}}%
	\KLResetSum%
}

%---------------------------------------------------------------------
% Display the Grand sum, and reset annual sum and the grand sum.
% Usage example in tabular:
%	\hline
%	\hline
%	Grand Total & \KLGrandSum \\
%	\hline
%--------------------------------------------------------------------
\newcommand{\KLGrandSum}{%
	\KLTabR{\Number{\arabic{KLGrandSumValue}}}%
	\KLResetGrandSum%
}

%---------------------------------------------------------------------
% Print the grand total of the costs to console.
%---------------------------------------------------------------------
\newcommand{\KLPrintGrandTotal}{%
	\typeout{}%
	\typeout{======= The total budget is \arabic{KLGrandTotalValue} yen. =======}%
	\typeout{}%
}

%============================================================
%  LaTeX macros to handle "efforts"		... sigh, who cares such numbers??
%============================================================

\newcounter{KLRemainingEffort}

%-----------------------------------------------------
\newcommand{\KLResetEffort}{
	\setcounter{KLRemainingEffort}{100}
}

%-----------------------------------------------------
\newcommand{\KLEffort}[1]{%
	#1%
	\addtocounter{KLRemainingEffort}{-#1}%
}

%====================================================================
% macros for making group_table.tex
%====================================================================
\newcounter{KLNumPeople}		% number of people in the group
\newcounter{KLDistBudgetSum}	% sum of distributed budget

\newcommand{\KLGname}[4]{%
	\begin{tabular}{l}%
		#1\\% kenkyusha ID
		#2\\% furigana
		#3\\% name in kanji
		age: #4 % age
	\end{tabular}%
	&%
	\ifthenelse{\equal{#3}{}}{% if blank, do nothing
	}{%
		\stepcounter{KLNumPeople}%
	}%
}

\newcommand{\KLGposition}[3]{%
	\begin{tabular}{l}%
		#1\\% institution
		#2\\% division
		#3% position
	\end{tabular}%
	&%
}

\newcommand{\KLGfield}[3]{%
	\begin{tabular}{l}%
		#1\\% research field
		#2\\%  degree
		#3% responsibility in this study
	\end{tabular}%
	&%
}

\newcommand{\KLGbudget}[1]{%
	\Number{#1} &% first year budget (k yen)
	\addtocounter{KLDistBudgetSum}{#1}
}

\newcommand{\KLGeffort}[1]{%
	#1 \\% effort for this study
}

%==================================================
%Fair License

%<Copyright Information>

%Usage of the works is permitted provided that this
%instrument is retained with the works, so that any entity
%that uses the works is notified of this instrument.

%DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.

%[2004, Fair License: rhid.com/fair]
%==================================================
% You may edit/modify this package at your own risk.
% If there are important fixes or changes that you think should be 
% reflected in the standard distribution, please notify:
%	taku@hep.sci.osaka-u.ac.jp  .
%==================================================
