Skip to content

Instantly share code, notes, and snippets.

@doraTeX
Last active July 30, 2025 10:59
Show Gist options
  • Save doraTeX/45f713758bf163380b924b9068ecb730 to your computer and use it in GitHub Desktop.
Save doraTeX/45f713758bf163380b924b9068ecb730 to your computer and use it in GitHub Desktop.
「可能なら一定の空白を空けて同行に、無理なら同行に右寄せで、それも無理なら次行の行末に」出力する命令
\documentclass[a4j,autodetect-engine,twocolumn]{jsarticle}
\usepackage[margin=20truemm]{geometry}
\columnseprule1pt
\makeatletter
\newbox\Answer@box
\newdimen\Answer@parindent
\NewDocumentEnvironment{Answer}{}{%
\par
\null
\nointerlineskip
\vskip-0.88zw
\Answer@parindent=\parindent
\begingroup
\setbox\Answer@box=\vbox\bgroup
\rightskip=-\leftskip
\hsize\linewidth
\parindent=\Answer@parindent
}{%
\par
\egroup
\unvbox\Answer@box
\endgroup
\par
\null
\nointerlineskip
\vskip\dimexpr\baselineskip-0.88zw\relax
}
\newbox\haiten@last
\newbox\haiten@nat
\newbox\haiten@tail
\newdimen\haiten@avail
\newdimen\haiten@gap
\newdimen\haiten@tailwd
%% \haiten[#1]{#2}
%% Answer環境内で用いる
%% 第1希望:現在の行に所定のスペース #1 を空けて #2 を出力
%% 第2希望:#1のスペースを空ける余裕がないときは,現在の行に右寄せで #2 を出力
%% 第3希望:右寄せでも #2 が入る余裕がないときは,改行して次の行の行末に #2 を出力
\NewDocumentCommand{\haiten}{O{0pt}m}{%
\unskip\unpenalty
\par% \lastbox確定
\setbox\haiten@last=\lastbox
\ifvoid\haiten@last\relax\else
\nointerlineskip
\setbox\haiten@nat=\hbox{\kern-\leftskip\unhcopy\haiten@last\kern-\rightskip}% 末尾行
\setbox\haiten@tail=\hbox{#2}%
\haiten@tailwd=\wd\haiten@tail
\haiten@gap=#1\relax
\haiten@avail=\dimexpr\linewidth-\wd\haiten@nat\relax
\ifdim\haiten@avail<\haiten@tailwd
% 第3希望:改行して次の行の行末に出力
\noindent
\box\haiten@nat
\nopagebreak
\par\noindent\hfill#2\par
\else
\ifdim\haiten@avail<\dimexpr\haiten@tailwd+\haiten@gap\relax
% 第2希望:同行内に右寄せで出力
\noindent
\unhcopy\haiten@nat
\hfill#2\par
\else
% 第1希望:同行内に所定のスペースを空けて出力
\noindent
\unhcopy\haiten@nat
\hskip\haiten@gap\hskip-\parfillskip\relax#2\par
\fi
\fi
\fi
}
\makeatother
%%% テスト用に深さの大きなインライン数式を用意
\def\testeq{$\displaystyle\frac{1}{\displaystyle\sum_{k=1}^n k^2}\int_0^1f(x)dx$}
%%% ランダムテストするための仕組み
\newcount\n
\def\printA#1{%
\ifnum#1>0
あ%
\expandafter\printA\expandafter{\the\numexpr#1-1\relax}%
\fi
}
\def\randomtest#1{%
\ifnum#1<100
\n=\pdfuniformdeviate 100
\printA{\n}%
\testeq となる。
\haiten[2zw]{……(1点)}
\par
\expandafter\randomtest\expandafter{\the\numexpr#1+1\relax}%
\fi
}
\begin{document}
\begin{Answer}
あああああああああああああああああああああああああ
\testeq となる。
\haiten[2zw]{……(1点)}
ああああああああああああああああああああああああああああ
\testeq となる。
\haiten[2zw]{……(1点)}
あああああああああああああああああああああああああああああ
\testeq となる。
\haiten[2zw]{……(1点)}
ああああああああああああああああああああああああああああああ
\testeq となる。
\haiten[2zw]{……(1点)}
あああああああああああああああああああああああああああああああ
\testeq となる。
\haiten[2zw]{……(1点)}
ああああああああああああああああああああああああああああああああ
\testeq となる。
\haiten[2zw]{……(1点)}
あああああああああああああああああああああああああああああああああ
\testeq となる。
\haiten[2zw]{……(1点)}
ああああああああああああああああああああああああああああああああああ
\testeq となる。
\haiten[2zw]{……(1点)}
%%% ランダムに100回テストしてみる
\randomtest{0}
\end{Answer}
\end{document}
@h-kitagawa
Copy link

h-kitagawa commented Jul 29, 2025

どうもありがとうございます.
LuaTeX-ja で試していたら,LuaTeX-ja の不具合を見つけました(後であっちに issue 立てます).
(2025-07-29 17:31 edit)→修正済

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment