Born in the town of Xi'e in Nanyang Commandery (north of the modern Nanyang City in Henan province), Zhang Heng came from a distinguished but not very affluent family. His grandfather Zhang Kan had been governor of a commandery and one of the leaders who supported the restoration of the Han by Emperor Guangwu (r. 25-57), following the death of the usurping Wang Mang of the Xin (AD 9-23). When he was ten, Zhang's father died, leaving him in the care of his mother and grandmother. An accomplished writer in his youth, Zhang left home in the year 95 to pursue his studies in the capitals of Chang'an and Luoyang. While traveling to Luoyang, Zhang passed by a hot spring near Mount Li and dedicated one of his earliest fu poems to it. This work, entitled Fu on the Hot Springs (Wēnquán fù 溫泉賦), describes the throngs of people attending the hot springs, which later became famous as the Huaqing Hot Springs, a favorite retreat of imperial concubine Yang Guifei during the Tang dynasty. After studying for some years at Luoyang's Taixue, he was well-versed in the classics and friends with several notable persons, including the mathematician and calligrapher Cui Yuan (78-143), the official and philosophical commentator Ma Rong (79-166), and the philosopher Wang Fu (78-163). Government authorities offered Zhang appointments to several offices, including a position as one of the Imperial Secretaries, yet he acted modestly and declined. At age twenty-three, he returned home with the title Officer of Merit in Nanyang, serving as the master of documents under the administration of Governor Bao De (in office from 103-111). As he was charged with composing inscriptions and dirges for the governor, he gained experience in writing official documents. As Officer of Merit in the commandery, he was also responsible for local appointments to office and recommendations to the capital of nominees for higher office. He spent much of his time composing rhapsodies on the capital cities. When Bao De was recalled to the capital in 111 to serve as a minister of finance, Zhang continued his literary work at home in Xi'e. Zhang Heng began his studies in astronomy at the age of thirty and began publishing his works on astronomy and mathematics.