Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.89 KB | None | 0 0
  1. catcode`@=11
  2.  
  3. begingroup
  4. catcode`P=12
  5. catcode`T=12
  6. lowercase{
  7. defx{defrem@pt##1.##2PT{##1ifnum##2>z@.##2fi}}}
  8. expandafterendgroupx
  9. defstrip@pt{expandafterrem@ptthe}
  10.  
  11.  
  12. defsetdimenzerotofontheightanddepth#1#2{
  13. dimen0=fontcharhtfont`#1
  14. advancedimen0 by fontchardpfont`#1
  15. ifx#2
  16. else
  17. advancedimen0 by fontcharhtfont`#2
  18. advancedimen0 by fontchardpfont`#2
  19. fi
  20. }
  21.  
  22. setdimenzerotofontheightanddepth fg
  23. edefdimzeroamt{strip@ptdimen0}
  24.  
  25. Dimen0 is: thedimen0, Baselineskip is: thebaselineskip
  26.  
  27. dimen1=baselineskip
  28. Unfortunately it also shows the dimzeroamount when I do the calculation: dividedimen1 by dimzeroamt
  29.  
  30. The ratio is: strip@ptdimen1
  31.  
  32. The real (manually calculated ratio) is $0.90947dots$
  33. catcode`@=12
  34.  
  35. bye
  36.  
  37. documentclass{article}
  38.  
  39. usepackage{graphics}
  40.  
  41. makeatletter
  42.  
  43. begin{document}
  44.  
  45.  
  46. defsetdimenzerotofontheightanddepth#1#2{
  47. dimen8=fontcharhtfont`#1
  48. advancedimen8 by fontchardpfont`#1
  49. ifx#2
  50. else
  51. advancedimen8 by fontcharhtfont`#2
  52. advancedimen8 by fontchardpfont`#2
  53. fi
  54. }
  55.  
  56. setdimenzerotofontheightanddepth fg
  57.  
  58. Dimen8 is: thedimen8, Baselineskip is: thebaselineskip
  59.  
  60.  
  61. Gscale@divtmp baselineskip{dimen8}
  62.  
  63. gives tmp
  64.  
  65. The real (manually calculated ratio) is $0.90947dots$
  66.  
  67.  
  68.  
  69. end{document}
  70.  
  71. dividedimen0 by <number>
  72.  
  73. input expl3-generic
  74.  
  75. ExplSyntaxOn
  76. cs_new_protected:Nn ioiooiioio_getfactor:Nnnn
  77. {% #1 is a control sequence, #2 the dimension, #3 and #4 two characters
  78. tl_set:Nx #1
  79. {
  80. fp_eval:n
  81. {
  82. dim_to_fp:n { #2 } /
  83. dim_to_fp:n
  84. {
  85. fontcharhtfont`#3 + fontcharhtfont`#4
  86. +
  87. fontchardpfont`#3 + fontchardpfont`#4
  88. }
  89. }
  90. }
  91. }
  92. cs_set_eq:NN getfactor ioiooiioio_getfactor:Nnnn
  93. ExplSyntaxOff
  94.  
  95. getfactortest{baselineskip}{f}{g}
  96.  
  97. test
  98.  
  99. bye
  100.  
  101. dim_max:nn { fontcharhtfont`#3 } { fontcharhtfont`#4 }
  102. +
  103. dim_max:nn { fontchardpfont`#3 } { fontchardpfont`#4 }
  104.  
  105. catcode`@=11
  106.  
  107. begingroup
  108. catcode`P=12
  109. catcode`T=12
  110. lowercase{
  111. defx{defrem@pt##1.##2PT{##1ifnum##2>z@.##2fi}}}
  112. expandafterendgroupx
  113. defstrip@pt{expandafterrem@ptthe}
  114.  
  115.  
  116. defsetdimenzerotofontheightanddepth#1#2{
  117. dimen8=fontcharhtfont`#1
  118. advancedimen8 by fontchardpfont`#1
  119. ifx#2
  120. else
  121. advancedimen8 by fontcharhtfont`#2
  122. advancedimen8 by fontchardpfont`#2
  123. fi
  124. }
  125.  
  126. setdimenzerotofontheightanddepth fg%
  127. edefdimzeroamt{strip@ptdimen8 %
  128. }%
  129.  
  130. dimen0=baselineskip%
  131. count0=65536
  132. loop%
  133. ifdimdimen0<8192p@%
  134. dimen0=2dimen0%
  135. dividecount0 by 2 %
  136. repeat
  137. dividedimen8count0
  138. dividedimen0dimen8
  139. strip@ptdimen0
  140. %edefx{strip@ptdimen0}
  141.  
  142. The real (manually calculated ratio) is $0.90947dots$%
  143. catcode`@=12
  144.  
  145. bye
  146.  
  147. 2^16 * numberdividend / numberdivisor
  148.  
  149. newdimenfnheight
  150. fnheight=13.19443pt baselineskip=12pt
  151.  
  152. {lccode`?=`p lccode`!=`t lowercase{gdefignorept#1?!{#1}}}
  153.  
  154. expandafterignoreptthe
  155. dimexprthenumexpr 65536*numberbaselineskip/numberfnheight sprelax
  156.  
  157. input apnum
  158.  
  159. newdimenfnheight
  160. fnheight=13.19443pt baselineskip=12pt
  161.  
  162. evaldefOUT{numberbaselineskip / numberfnheight}
  163.  
  164. OUT % OUT=.90947485284083681234 because apFRAC=20 by default.
  165.  
  166. bye
  167.  
  168. makeatletter
  169. newcommandSetToRatio[3]{% sets #1 to be the ratio #2/#3, where #2 and #3
  170. % are lengths (registers or expressions).
  171. % The ratio #2/#3 should evaluate to less than 16384 in absolute value to
  172. % avoid arithmetic overflow. It will be computed as fixed point
  173. % number with about 4 or 5 digits after decimal mark.
  174. edef #1%
  175. {strip@ptdimexpr
  176. numexprdimexpr#2relax*65536/dimexpr#3relaxrelax sprelax}%
  177. }
  178. makeatother
  179.  
  180. SetToRatio{foo}{20000sp}{1sp}
  181.  
  182. % j'avais fait cela dans https://tex.stackexchange.com/a/328894/4686
  183.  
  184. % Octobre 2016
  185.  
  186. documentclass{article}
  187.  
  188. makeatletter
  189.  
  190. countdefddf@cnta=z@
  191. countdefddf@cntb=tw@
  192. countdefddf@cntc=4
  193. countdefddf@cntd=6
  194.  
  195. % % if used with Plain TeX, un-comment this
  196. % % LaTeX loop or any loop allowing elserepeat:
  197. %
  198. % longdefloop #1repeat{%
  199. % def iterate {#1relax expandafter iterate fi}iterate
  200. % let iterate relax }
  201.  
  202. % defdivdef #1#2#3{% if using plain tex
  203.  
  204. newcommanddivdef [3]{%
  205. % description:
  206. % computes R = #2/#3 as nearest multiple of 1/65536 (ties go to even)
  207. % then define the macro #1 to be the decimal expansion of this up to five digits
  208. % after decimal mark.
  209. begingroup
  210. dimen@ #3relax % denominator
  211. dimen@ii #2relax % numerator
  212. ifdimdimen@<z@
  213. dimen@-dimen@
  214. dimen@ii-dimen@ii
  215. fi
  216. ifdimdimen@ii<z@
  217. defddf@sgn{-}dimen@ii-dimen@ii
  218. else
  219. letddf@sgnempty % no @empty in Plain !
  220. fi
  221. ddf@cntadimen@ % non negative denominator (we hope non zero...)
  222. ddf@cntbdimen@ii % non negative numerator
  223. divideddf@cntbddf@cnta % integer part of ratio, will be stored in ddf@cntd
  224. ddf@cntdddf@cntb
  225. multiplyddf@cntb-ddf@cnta % no overflow possible because TeX's division truncates
  226. advanceddf@cntbdimen@ii % now numerator in ddf@cntb is < denom
  227. count@z@ % will store fractional part as a multiple of sp's
  228. ifnumddf@cntb>z@
  229. ifnumddf@cnta>32768relax
  230. ddf@cnta65536relax
  231. loop
  232. ddf@cntcdimen@ % denominator
  233. advanceddf@cntc-ddf@cntb
  234. ifnumddf@cntb<ddf@cntc
  235. divideddf@cntatw@
  236. advanceddf@cntbddf@cntb
  237. else
  238. ifnumddf@cntb=ddf@cntc
  239. divideddf@cntatw@
  240. advancecount@ddf@cnta
  241. ddf@cntaz@ % abort the loop here
  242. else
  243. advancecount@ddf@cnta % not same order as in previous branch!
  244. divideddf@cntatw@
  245. ddf@cnta-ddf@cnta
  246. advanceddf@cntcddf@cntc
  247. ddf@cntbddf@cntc
  248. fi
  249. fi
  250. ifnumddf@cnta=z@else % signed quantity: can not do if foo>z@ ...
  251. repeat
  252. % it is possible here that count@ is 65536
  253. % in case of a tie at the last unit the rounding was to even!
  254. else
  255. % here denom <= 2^15=32768 (=0.5pt), hence 65536num <= 2^31 - 65536
  256. multiplyddf@cntb65536relax
  257. % extra steps to do rounding
  258. ddf@cntcddf@cnta
  259. divideddf@cntctw@
  260. advanceddf@cntbddf@cntc % no overflow possible
  261. ddf@cntcddf@cntb % need to keep copy for later branch
  262. divideddf@cntcddf@cnta
  263. count@ddf@cntc
  264. ifoddddf@cnta
  265. % odd denom, no tie possible
  266. else
  267. multiplyddf@cntcddf@cnta
  268. ifnumddf@cntb=ddf@cntc
  269. % implement "ties go to even", the rounding was "up"
  270. ifoddcount@advancecount@m@nefi
  271. fi
  272. fi
  273. % to get count@ 65536 we would need to have N/D >= 65535.5/65536
  274. % i.e. N/D >= 1 - 1/131072, but N/D<= 1 - 1/D, D<=32768, hence
  275. % despite the rounding this branch always produces count@ < 65536.
  276. fi
  277. fi
  278. dimen@count@ sprelax
  279. % (thecount@) % debug check
  280. expandafterdivdef@endthedimen@ #1%
  281. }
  282. begingroup
  283. catcode`P 12
  284. catcode`T 12
  285. lowercase{gdefdivdef@end #1.#2PT}#3{%
  286. advanceddf@cntd #1relax % almost always #1=0
  287. ifnum#2>z@
  288. edefx{endgroupedefnoexpand#3{ddf@sgntheddf@cntd.#2}}%
  289. else
  290. ddf@cntdddf@sgnddf@cntd
  291. edefx{endgroupedefnoexpand#3{theddf@cntd}}%
  292. fi
  293. x
  294. }endgroup
  295.  
  296. begin{document}
  297.  
  298. ttfamily
  299.  
  300. divdefFOO{20000sp}{1sp}
  301.  
  302. meaningFOO
  303.  
  304. divdefFOO{355pt}{113pt}
  305.  
  306. meaningFOO
  307.  
  308. divdefFOO{1pt}{7pt}
  309.  
  310. meaningFOO
  311.  
  312. divdefFOO{10000pt}{7pt}
  313.  
  314. meaningFOO
  315.  
  316. divdefFOO{1000000000sp}{7sp}
  317.  
  318. meaningFOO
  319.  
  320. end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement