I am a Pakistani national living in mainland China. Since a visa is mandatory for Pakistani citizens, how do I apply for a Hong Kong visa from mainland China?
The link you provide has the answer. In Arrangements for Entry to the Hong Kong Special Administrative Region (HKSAR) from the Mainland China, it says:
Holders of foreign passports who are living in the Mainland may submit appropriate application forms relating to the purpose of entry to Hong Kong (other than entry under the Immigration Arrangements for Non-local Graduates, Capital Investment Entrant Scheme Note 7, Quality Migrant Admission Scheme, Admission Scheme for the Second Generation of Chinese Hong Kong Permanent Residents or Technology Talent Admission Scheme) together with the relevant supporting documents to the Immigration Division of the Office of the Government of the HKSAR in Beijing (Beijing Office) or the Immigration Division of the Hong Kong Economic and Trade Office in Shanghai (SHETO). Applicants are required to produce their valid travel documents to the Immigration Division of the Beijing Office or the Immigration Division of the SHETO so that a visa/entry permit could be issued if the application is approved. The addresses of the two Mainland offices are :
The Office of the Government of the HKSAR in Beijing No. 71, Di'anmen Xidajie, Xicheng District, Beijing 100009, People's Republic of China.:
The Hong Kong Economic and Trade Office in Shanghai 21/F, The Headquarters Building, 168 Xizang Road (M), Huangpu District Shanghai 200001 People's Republic of China.
For holders of foreign passports who are living outside Beijing or Shanghai, they may send completed application forms by post directly to the Immigration Department to the address in paragraph 24 or through their sponsors in Hong Kong.